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

US Mercari iOS App Architecture and Dependency ...

mercari
September 30, 2017
2k

US Mercari iOS App Architecture and Dependency Injection

mercari

September 30, 2017
Tweet

Transcript

  1. Mercari Tech Conf 2017 Software Engineer (iOS) Yoichi Tagaya US版Mercari

    iOSアプリの アーキテクチャーとDependency Injection US Mercari iOS App - Architecture and Dependency Injection
  2. About Me Yoichi Tagaya Author of Swinject - Dependency injection

    framework - 1800+ GitHub stars - 5 maintainers - De facto standard in Swift @yoichitgy
  3. Swinject Users (Job Descriptions) - Atlassian - Implement unit tests

    and performance tests in Objective-C and Swift using XCTest, OCMock, Swinject... - https://www.smartrecruiters.com/Atlassian/93336663-ios-developer - MealPass - Familiarity with dependency injection libraries such as swinject - http://garysguide.com/jobs/up0gwed/Software-Engineer-at-MealPass-New-York-NY - Location Labs - We are using in our iOS products, including Cocoapods, Typhoon/Swinject, Realm, PromiseKit, Alamofire, Amplitude... - https://www.latitude.work/locationlabs/emeryville/senior-software-engineer-ios - МАДЖЕСТИ - 1. Realm, 2. Dependency injection (Swinject), 3. Работа с платежными... - https://voronezh.hh.ru/vacancy/17552731 - And more… (Mercari too)
  4. About Me at Mercari Yoichi Tagaya iOS Engineer - Mainly

    US Mercari App - Since January, 2017
  5. New Architecture: MVVM + Repository Pattern Model View ViewModel Service

    Repository Entity UIView/UIViewController Protocol Buffers Class with Protocol Class with Protocol Reference Signal (ReactiveSwift)
  6. New Architecture: MVVM + Repository Pattern Model View ViewModel Service

    Repository Entity UIView/UIViewController Protocol Buffers Class with Protocol Class with Protocol Class with Protocol Class with Protocol Reference Signal (ReactiveSwift) Use protocol as interface
  7. So, You Know... Dependency Injection Inversion of Control for resolving

    dependencies You create what you use. ↺ What you use are created and passed.
  8. More Advantages of Dependency Injection 1. Use dependency without its

    knowledge 2. Clarify design of dependency 3. Promote modularization 4. Avoid vendor lock in
  9. Dependency Injection in WWDC • WWDC 2016 • Improving Existing

    Apps with Modern Best Practices* • WWDC 2017 (indirectly) • Engineering for Testability** * https://developer.apple.com/videos/play/wwdc2016/213/ ** https://developer.apple.com/videos/play/wwdc2017/414/
  10. Wait, What’s DI (Dependency Injection) Container? DI Container Register the

    dependency graph A B C D E F G H I Dependency graph let a = … Resolve dependencies upon your request
  11. How to Use Swinject import Swinject let container = Container()

    container.register(Animal.self) { _ in Cat() } container.register(PetOwner.self) { resolver in PetOwner(pet: resolver.resolve(Animal.self)!) } DI Container Register Use resolver to resolve inside a closure
  12. How to Practically Organize Dependency Definitions • Use Assembly and

    Assembler • Production app has too many dependencies to define at once
  13. final class HomeAssembly: Assembly { func assemble(container: Container) { container.register(HomeViewModelProtocol.self)

    { resolver in let dependency = ( resolver.resolve(APIClientProtocol.self)!, resolver.resolve(CrashlyticsAdapterProtocol.self)! ) return HomeViewModel(dependency: dependency) } container.register(HomeViewController.self) { resolver in let vc = HomeViewController.instantiateFromStoryboard() vc.viewModel = resolver.resolve(HomeViewModelProtocol.self)! return vc } } } How to Use Assembly Group related dependency definitions by Assembly
  14. let container = Assembler([ // Home HomeAssembly(), // ItemDetail ItemDetailAssembly(),

    // APIClient APIClientAssembly(), // Util DateAssembly(), // Adapter CrashlyticsAssembly(), FirebaseAssembly(), ]).resolver How to Use Assembler Assembler Container (as Resolver) Array of Assemblies
  15. Summary • Architecture: MVVM + Repository Pattern • Dependency Injection:

    Mendokusai ~ Tiresome ~ • Dependency Injection Container X
  16. Thank You Feel free to talk to me in the

    other room for questions about this talk or iOS engineering at Mercari