Skip to content

Latest commit

 

History

History
62 lines (46 loc) · 1.19 KB

README.md

File metadata and controls

62 lines (46 loc) · 1.19 KB

Swift Dependency Injector

How to use

For development module

class DevModule: AbstractModule {
  override func configure() {
    super.configure()

    bind(String.self).withName("api-host").to("http://api.development.com")

    bind(FooService.self).to(DevFooServiceImpl())
    bind(BarService.self).to(BarServiceImpl())
  }
}

For testing module

class TestModule: DevModule {
  override func configure() {
    super.configure()

    bind(String.self).withName("api-host").to("http://api.localhost.com")

    bind(BarService.self).to(FakedServiceImpl())
  }
}

For production module

class ProdModule: DevModule {
  override func configure() {
    super.configure()

    bind(String.self).withName("api-host").to("http://api.production.com")

    bind(FooService.self).to(ProdFooServiceImpl())
  }
}

In app delegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  Injectors.initialize(modules: DevModule())

  //...
  return true
}

get instance

  let fooService = Injectors.get(FooService.self)
  fooService.foo()