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

layered arch with swinject

layered arch with swinject

potatotips#34で話した際に使ったスライドです。iOSの話です。

Shinnosuke Kugimiya

October 26, 2016
Tweet

More Decks by Shinnosuke Kugimiya

Other Decks in Technology

Transcript

  1. %*͡Όͳ͍࣌OFX class PokemonListViewController: UIViewController { var pokemonList: [Pokemon] = []

    override func viewDidLoad() { let repository = RealmPokemonRepository() pokemonList = repository.findAll() } }
  2. %*͡Όͳ͍࣌OFX class PokemonListViewController: UIViewController { var pokemonList: [Pokemon] = []

    override func viewDidLoad() { let repository = RealmPokemonRepository() pokemonList = repository.findAll() } }
  3. %*͡Όͳ͍࣌TJOHMFUPO class PokemonListViewController: UIViewController { var pokemonList: [Pokemon] = []

    override func viewDidLoad() { let repository = RealmPokemonRepository.sharedInstance pokemonList = repository.findAll() } }
  4. %*͡Όͳ͍࣌TJOHMFUPO class PokemonListViewController: UIViewController { var pokemonList: [Pokemon] = []

    override func viewDidLoad() { let repository = RealmPokemonRepository.sharedInstance pokemonList = repository.findAll() } }
  5. %* class PokemonListViewController: UIViewController { var repository: RealmPokemonRepository? var pokemonList:

    [Pokemon]? override func viewDidLoad() { pokemonList = repository?.findAll() } }
  6. %* class PokemonListViewController: UIViewController { var repository: RealmPokemonRepository? var pokemonList:

    [Pokemon]? override func viewDidLoad() { pokemonList = repository?.findAll() } }
  7. ૚ؒͷґଘ͕ૄͱ͸ w Լͷ૚͸্ͷ૚ʹґଘ͠ͳ͍ ·ͬͨ͘஌Βͳ͍  w ্ͷ૚͸Լͷ૚ͷJOUFSGBDF QSPUPDPM ͔͠஌Βͳ͍ w

    64#Λྫʹ͢ΔͱΘ͔Γ΍͍͔͢΋ʁ
 64#Λ࢖͏ଆ͸ܗͱϓϩτίϧ͚ͩ஌͍ͬͯΕ͹ɺ 64#ଆ͸Կ͕ͬͯ͞͞དྷΔͷ͔શ͘஌Βͳ͍Ͱ 0,ɻ  w ͜͜ʹ%*Λ࢖͏ͱྑ͍
  8. *OGSB$POUBJOFS class InfraContainer { static func register(container: Container) { container.register(UserBehaviorEventRepository.self)

    { _ in RealmUserBehaviorEventRepository() } container.register(ContentsLoader.self) { _ in InternalContentsLoader() } container.register(AppUpdateChecker.self) { _ in NetworkAppVersionChecker() } container.register(RealmInitializer.self) { r in return RealmInitializer(contentsLoader: r.resolve(ContentsLoader.self)!) } } }
  9. *OGSB$POUBJOFS class InfraContainer { static func register(container: Container) { container.register(UserBehaviorEventRepository.self)

    { _ in RealmUserBehaviorEventRepository() } container.register(ContentsLoader.self) { _ in InternalContentsLoader() } container.register(AppUpdateChecker.self) { _ in NetworkAppVersionChecker() } container.register(RealmInitializer.self) { r in return RealmInitializer(contentsLoader: r.resolve(ContentsLoader.self)!) } } } w ఏڙ͍ͯ͠ΔQSPUPDPM
  10. *OGSB$POUBJOFS class InfraContainer { static func register(container: Container) { container.register(UserBehaviorEventRepository.self)

    { _ in RealmUserBehaviorEventRepository() } container.register(ContentsLoader.self) { _ in InternalContentsLoader() } container.register(AppUpdateChecker.self) { _ in NetworkAppVersionChecker() } container.register(RealmInitializer.self) { r in return RealmInitializer(contentsLoader: r.resolve(ContentsLoader.self)!) } } } w ࣮ࡍʹ࢖ΘΕΔ۩ମΫϥε
  11. ॳظԽ @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var injector: Injector?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let container: Container = Container() InfraContainer.register(container) DomainContainer.register(container) UseCaseContainer.register(container) injector = Injector(container: container) : } : } ֤ϨΠϠʔͷந৅۩ମϚοϓΛొ࿥
  12. ॳظԽ @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var injector: Injector?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let container: Container = Container() InfraContainer.register(container) DomainContainer.register(container) UseCaseContainer.register(container) injector = Injector(container: container) : } : } *OKFDUPSΛ࡞੒
  13. *OKFDUPS w Կ͔ʹґଘ͍ͯ͠ΔΦϒδΣΫτʹґଘ͍ͯ͠Δ΋ͷΛηοτͯ͠ ͋͛Δਓ w *OKFDUPS͸DPOUBJOFSΛϝϯόͱͯ࣋ͭ͠ w 7JFX$POUSPMMFSͳͲґଘͯ͠ΔଆຖʹͦΕΛҾ਺ͱͨ͠JOKFDUϝιο υΛ͍࣋ͬͯΔ w

    *OKFDUPS͸"QQ%FMFHBUF͕࣋ͭ w 7JFX$POUSPMMFSͷWJFX%JE-PBEͳͲͰ BQQ%FMFHBUFJOKFDUPSJOKFDU TFMG Λݺͼग़͢͜ͱͰϝϯόʔ͕JOKFDU ͞ΕΔ
  14. *OKFDUPS class Injector { private let container: Container init(container: Container)

    { self.container = container } func inject(inputViewController: InputViewController) { inputViewController.userBehaviorEventRepository = container.resolve(UserBehaviorEventRepository.self) } func inject(resultViewController: ResultViewController, pokemon: Pokemon) { resultViewController.userBehaviorEventRepository = container.resolve(UserBehaviorEventRepository.self) } } *OKFDUPS͸*OQVU7JFX$POUSPMMFSΛҾ਺ʹ࣋ͬͨJOKFDUϝιο υΛ࣋ͪɺͦ͜Ͱґଘ͍ͯ͠Δ΋ͷΛηοτͯ͋͛͠Δ