Swift Package Index


Introduce dependency injection attribute (@Inject)


  • The latest stable release is 0.0.1. Released over 1 year ago.
  • The last commit to master was over 1 year ago.

Swift Version Compatibility

  • 0.0.1
    5.3
    5.2
    5.1
    5.0
    4.2
  • master
    5.3
    5.2
    5.1
    5.0
    4.2
Full build results

Platform Compatibility

  • 0.0.1
    iOS
    macOS(Intel)
    macOS(ARM)
    Linux
    tvOS
    watchOS
  • master
    iOS
    macOS(Intel)
    macOS(ARM)
    Linux
    tvOS
    watchOS
Full build results

DIAttribute

[WIP] DIAttribute introduces dependency injection attribute.

Installation

You can install this framework with Swift Package Manager in Xcode 11.

Feature

Inject protocol values

final class ViewController: UIViewController {
    @Inject(Self.self) var apiClient: APIClientProtocol
    
    ...
}

// Production
DIResolver.register(ViewController.self, keyPath: \.apiClient, value: ProductionAPIClient())

// Test
DIResolver.register(ViewController.self, keyPath: \.apiClient, value: MockAPIClient())

Inject multiple values

struct Environment {
    @Inject(Self.self) var endpoint: URL
    @Inject(Self.self) var timeZone: TimeZone
}

DIResolver.register(Environment.self) {
    Register(URL.self, URL(string: "https://example.com")!)
    Register(TimeZone.self, TimeZone(identifier: "Asia/Tokyo")!)
}

License

DIAttribute is released under the MIT license. See LICENSE for details.