DIY DI

Ddd6d3bac7772fa67fc5e312a18bdaec?s=47 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.

Ddd6d3bac7772fa67fc5e312a18bdaec?s=128

sammyd

March 23, 2018
Tweet

Transcript

  1. DIY DI sam : @iwantmyrealname github.com/sammyd/iOSCon2018_DIYDI

  2. single tons

  3. single tons

  4. one instance

  5. network controller

  6. file access

  7. animation engine

  8. what's the problem?

  9. global access

  10. class Manager { static let shared = Manager() private init()

    }
  11. class Manager { static let shared = Manager() private init()

    } let manager = Manager.shared
  12. bad

  13. hidden state

  14. hard to test

  15. possible solution...

  16. don't allow global access

  17. give objects what they need

  18. inject their dependencies

  19. hierarchy problem

  20. None
  21. None
  22. None
  23. None
  24. None
  25. None
  26. alternative

  27. a way to find dependencies

  28. None
  29. contained facsimile for global state

  30. dependency container

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

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

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

    protocol DataStoreProvider { var dataStore: DataStore { get } } protocol ViewControllerFactory { func createBeautifulVC() -> UIViewController ... }
  34. class DependencyContainer: NetworkProvider, DataStoreProvider, ViewControllerFactory { ... }

  35. class MyViewController { typealias DependencyProvider = NetworkManagerProvider & ViewControllerFactory ...

    }
  36. class MyViewController { typealias DependencyProvider = NetworkManagerProvider & ViewControllerFactory private

    let provider: DependencyProvider ... }
  37. class MyViewController { typealias DependencyProvider = NetworkManagerProvider & ViewControllerFactory private

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

    let provider: DependencyProvider private lazy var dataStore = provider.dataStore init(dependencyProvider: DependencyProvider) { self.dependencyProvider = dependencyProvider } }
  39. by no means perfect

  40. but better than before

  41. DI/IoC popular with the grownups

  42. consider it when you're struggling

  43. @iwantmyrealname github.com/sammyd/iOSCon2018_DIYDI