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

VIPER Architecture から学ぶ Dependency Injection

VIPER Architecture から学ぶ Dependency Injection

iOS Test Night #5 - connpass https://testnight.connpass.com/event/59283/

71ce746e94f84be1cef8e57e8277bd6d?s=128

star__hoshi

July 26, 2017
Tweet

Transcript

  1. VIPER Architecture ͔ΒֶͿ Dependency Injection iOS Test Night #5

  2. ࣗݾ঺հ • ຊ໊: ੕઒݈հ • Github: starhoshi • Twitter: @star__hoshi

    • Classi גࣜձࣾ • -> 8/1 ͔Β ʁʁʁ
  3. VIPER ͷ Router ʹ͍ͭͯ IUUQTDIFFTFDBLFMBCTDPNCMPHJPTQSPKFDU BSDIJUFDUVSFVTJOHWJQFS

  4. ͬ͘͟Γ VIPER • View • Interactor • Presenter • Entity

    • Router
  5. ͬ͘͟Γ VIPER • View - UIViewController • Interactor - UseCase(Business

    Logic) • Presenter - View ͱ Interactor Λ஥հ • Entity - Data Structure • Router - ը໘ભҠͱ Dependency Injection
  6. ͬ͘͟Γ VIPER • View - UIViewController • Interactor - UseCase(Business

    Logic) • Presenter - View ͱ Interactor Λ஥հ • Entity - Data Structure • Router - ը໘ભҠͱ Dependency Injection
  7. Router ͷ໾ׂ • ը໘ભҠ • ը໘ભҠͱ Dependency Injection

  8. ͜Μͳ͔Μ͡ IUUQTDIFFTFDBLFMBCTDPNCMPHJPTQSPKFDU BSDIJUFDUVSFVTJOHWJQFS

  9. ͕࣌ؒͳ͍ͷͰ ཧղ͠΍͘͢͢ΔͨΊ View, Presenter, Router ͚ͩݟ͍͖ͯ·͢

  10. View protocol UserView: class { var presenter: UserPresentation { get

    } init(presenter: UserPresentation) } final class UserViewController: UIViewController, UserView { let presenter: UserPresentation init(presenter: UserPresentation) { self.presenter = presenter } }
  11. Presenter protocol UserPresentation: class { var router: UserWireframe { get

    } init(router: UserWireframe) } final class UserPresenter: UserPresentation { let router: UserWireframe init(router: UserWireframe) { self.router = router } }
  12. Router protocol UserWireframe: class { static func assembleModule() -> UIViewController

    } final class UserRouter: UserWireframe { static func assembleModule() -> UIViewController { let router = UserRouter() let presenter = UserPresenter(router: router) let view = UserViewController(presenter: presenter) return view } }
  13. ؔ܎ੑ • ந৅ʹґଘ͍ͯ͠Δ • ͦΕͧΕ͕ Protocol Λ࣮૷͠ɺ Protocol ʹґଘ͍ͯ͠Δ •

    Router ͚͕ͩ۩৅ΫϥεΛ Initialize ͍ͯ͠Δ • Router Ͱ DI ͍ͯ͠Δʂ
  14. DI ͷୈҰา final class UserPresenter: UserPresentation { let router: UserWireframe

    init(router: UserWireframe) { self.router = router } } final class UserPresenter: UserPresentation { let router: UserWireframe init() { self.router = UserRouter() } } ❌ ⭕
  15. Կ͕خ͍͠ͷ͔

  16. ςετ͕ॻ͖΍͘͢ͳͬͨ • Protocol ʹͷΈґଘ͍ͯ͠Δ • Mock ΫϥεΛ࡞ΕΔ • ґଘؔ܎ͷগͳ͍ Testable

    ͳΞʔΩςΫνϟʹ • ଞͷΞʔΩςΫνϟͰ΋Ԡ༻Ͱ͖Δ
  17. ςετίʔυ class UserPresenterSpec: QuickSpec { override func spec() { let

    router = MockUserRouter() let presenter = UserPresenter(router: router) describe("viewDidLoad") { beforeEach { presenter.viewDidLoad() } it("…") { … } } } }
  18. ͖ͬ͞ͷίʔυ • ͍Ζ͍Ζ୺ંͬͯΔ • Presenter ͔Β View ʹ఻͑ΒΕͳ͍ͱ͔ • Router

    ͕ը໘ભҠͰ͖ͳ͍ͱ͔ • ͦ΋ͦ΋ίϯύΠϧ௨Βͳ͍
  19. ਅ໘໨ʹ VIPER ͨ͠ίʔυ IUUQTHJUIVCDPNTUBSIPTIJ%FWJDF8BMMQBQFS

  20. AppStore ʹ΋ग़ͯΔ

  21. None
  22. https://iosdc.jp/2017/node/1543 Stub ͱ Mock ͷҧ͍ɺͪΌΜ ͱཧղͰ͖͍ͯ·͔͢ʁ

  23. VIPER Architecture ͔ΒֶͿ Dependency Injection

  24. Ҏ্