Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DIY DI

sammyd
March 23, 2018

DIY DI

The grown up languages use dependency injection all the time. This talk takes a look at a problem I was experiencing in an iOS app, and then a quick run through a potential (evolving) solution.

sammyd

March 23, 2018
Tweet

More Decks by sammyd

Other Decks in Programming

Transcript

  1. bad

  2. protocol NetworkManagerProvider { var networkManager: NetworkManager { get } }

    protocol DataStoreProvider { var dataStore: DataStore { get } }
  3. protocol NetworkManagerProvider { var networkManager: NetworkManager { get } }

    protocol DataStoreProvider { var dataStore: DataStore { get } } protocol ViewControllerFactory { func createBeautifulVC() -> UIViewController ... }
  4. class MyViewController { typealias DependencyProvider = NetworkManagerProvider & ViewControllerFactory private

    let provider: DependencyProvider private lazy var dataStore = provider.dataStore ... }
  5. class MyViewController { typealias DependencyProvider = NetworkManagerProvider & ViewControllerFactory private

    let provider: DependencyProvider private lazy var dataStore = provider.dataStore init(dependencyProvider: DependencyProvider) { self.dependencyProvider = dependencyProvider } }