Skip to content

Simple dependency injection library for swift projects

License

Notifications You must be signed in to change notification settings

vadimvitvickiy/SwiftDI

 
 

Repository files navigation

SwiftDI

Github Actions Travis CI CocoaPods Version License Platforms Swift Version Swift Package Manager compatible

Simple dependency injection library for swift projects.

Example

Assemble dependencies

Basically SwiftDI works as Service locator. It stores all dependencies inside own list. But Injector can be easily splited into submodules to reduce code linking.

// Swift
let injector = Injector()

injector.bind(ApiProtocol.self)
    .with { return RestApi() }

injector.bind(RepositoryProtocol.self)
    .with { (api: ApiProtocol) -> RepositoryProtocol.self in
        return Repository(api: api) 
    }

// or you can bind Repository constructor directly
injector.bind(RepositoryProtocol.self)
    .with(Repository.init(api:))

Inject dependencies to ViewController directly

// Swift
class Controller: UIViewController {

  var injector: Injector! = ... // provide injector to VC
  var repository: RepositoryProtocol!

  func viewDidLoad() {
      super.viewDidLoad()
      repository = injector.resolve()
  }
}

Dependency injection options

// Swift
injector.bind(ApiProtocol.self) // link with type
    .tag("some tag")            // link with custom string tag
    .scope(.singleton)          // define dependency as singleton
    .with { ... }               // initilization dependency closure
    
injector.resolve(ApiProtocol.self, tag: "some value") //resolving by type and tag 

Submodules

Injector support modularization and could have isollated submodules that know about parent but don't know about siblings.

// Top level module
let injector = Injector()

injector.bind(RepositoryProtocol.self)
    .with { injector -> RepositoryProtocol.self in
         return Repository()
    }

// Screen module
let childInjector = injector.plus()
childInjector.bind(PresenterProtocol.self)
    .with { injector -> PresenterProtocol in 
        return Presenter(repository: injector.resolve())
    }

CocoaPods

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. For setup you have to visit their website. Once you have set up your project for CocoaPods you can specify SwiftDI dependency in your Podfile:

pod 'SwiftDI', '~> 2.0'

Swift Package Manager

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Once you have your Swift package set up, adding SwiftDI as a dependency is as easy as adding it to the dependencies value of your Package.swift:

Swift 5

dependencies: [
    .package(url: "https://github.com/achernoprudov/SwiftDI.git", from: "2.0.0")
]

About

Simple dependency injection library for swift projects

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.5%
  • Ruby 1.3%
  • C 0.2%