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

Lightweight Dependency Injection tips

Lightweight Dependency Injection tips

Motoki Narita

May 26, 2017
Tweet

More Decks by Motoki Narita

Other Decks in Technology

Transcript

  1. final class WebViewController: UIViewController, DependencyInjectable { private var url: URL!

    typealias Dependency = URL static func make(withDependency dependency: Dependency) -> WebViewController { let vc = WebViewController.instantiateInitialViewController(with: "WebViewController") vc.url = dependency return vc } } ґଘ͢ΔΦϒδΣΫτͷఆٛ
  2. final class WebViewController: UIViewController, DependencyInjectable { private var url: URL!

    typealias Dependency = URL static func make(withDependency dependency: Dependency) -> WebViewController { let vc = WebViewController.instantiateInitialViewController(with: "WebViewController") vc.url = dependency return vc } } %*ͳ7JFX$POUSPMMFSͷΠϯελϯε͸ɺ NBLFϝιουͰੜ੒͢Δ
  3. override func prepare(for segue: UIStoryboardSegue, sender: Any?) { switch segue.destination

    { case let vc as WebViewController: vc.url = url default: break } }  VSMΛ֎෦ʹެ։͢Δඞཁ͕͋Δ  ͜ͷ7JFX$POUSPMMFSΛݺͼग़͢৔ॴ͕૿͑ͨ৔߹ɺͦͷ౎౓͜ͷίʔυΛ ॻ͘ඞཁ͕͋Δ
  4. final class WebViewController: UIViewController, DependencyInjectable { private var url: URL!

    typealias Dependency = URL static func make(withDependency dependency: Dependency) -> WebViewController { let vc = WebViewController.instantiateInitialViewController(with: "WebViewController") vc.url = dependency return vc } }  VSMΛQSJWBUFʹͰ͖Δ  ΠϯελϯεԽͷํ๏͕౷Ұ͞Ε͍ͯΔ͜ͱͰ%3:ݪଇΛकΕΔ
  5. final class WebViewController: UIViewController, DependencyInjectable { private var url: URL!

    typealias Dependency = URL static func make(withDependency dependency: Dependency) -> WebViewController { let vc = WebViewController.instantiateInitialViewController(with: "WebViewController") vc.url = dependency return vc } } ͜Ε͚ͩݟΔͱ4FHVFͷํ͕ศརʹݟ͑Δ͕ɺ΋ͬͱෳࡶͳґଘੑ Λ஫ೖ͢Δඞཁ͕Ͱ͖ͨ৔߹ʹɺ4FHVFΛ࢖ͬͨํࣜ͸͔ͳΓ໘౗ ͳ͜ͱʹͳΔ
  6. final class WebViewController: UIViewController, DependencyInjectable { private var url: URL!

    private var customUserAgent: String? struct Dependency { let url: URL let customUserAgent: String? } static func make(withDependency dependency: Dependency) -> WebViewController { let vc = WebViewController.instantiateInitialViewController(with: "WebViewController") vc.url = dependency.url vc.customUserAgent = dependency.customUserAgent return vc } } ྫͱͯ͠͸ɺ8FC7JFXʹઃఆ͢ΔDVTUPN6TF"HFOUΛมߋ͢Δඞཁ ͕͋Δ৔߹ɺ͜ͷΑ͏ʹ%FQFOEFODZTUSVDUΛఆٛͰ͖ɺNBLFϝ ιου಺Ͱ׬݁ͤ͞Δ͜ͱ͕Ͱ͖Δ
  7. extension UIViewController { func present<DI: DependencyInjectable>(viewControllerType: DI.Type, dependency: DI.Dependency, withNavigationType

    navigationType: UINavigationController.Type? = nil, animated: Bool = true, completion: (() -> Swift.Void)? = nil) where DI: UIViewController { let vc = viewControllerType.make(withDependency: dependency) if let navigationType = navigationType { let nav = navigationType.init(rootViewController: vc) present(nav, animated: animated, completion: completion) } else { present(vc, animated: animated, completion: completion) } } func push<DI: DependencyInjectable>(viewControllerType: DI.Type, dependency: DI.Dependency, animated: Bool = true) where DI: UIViewController { let vc = viewControllerType.make(withDependency: dependency) navigationController?.pushViewController(vc, animated: animated) } } %FQFOEFODZ*OKFDUBCMFΛ࢖ͬͯભҠ࣌ͷίʔυΛந৅Խ