Share
Embed
Lightweight Dependency Injection tips
Motoki Narita
May 26, 2017
Transcript
38 slides
-
ࣗݾհ w !NPUPLJFF w J04ΤϯδχΞ w ϝϧΧϦικॴଐ w ϝϧΧϦΧϧνʔϜ w
J04%$+BQBOελοϑ -
͜͜Ͱհ͞Ε͍ͯΔํ๏ͷΑ͘ͳ͍ͱ͜Ζ w 4FHVFͰભҠΛߦ͏߹ɺ֤ॴͰґଘΦϒδΣΫτ Λ͢ඞཁ͕͋Δ w %3:ʹͳΓʹ͍͘ w ඞཁͳΦϒδΣΫτΛ͞ͳͯ͘ίϯύΠϥ͕ ΤϥʔΛग़ͯ͘͠Εͳ͍ w
ˠҙਤ͠ͳ͍༻్ͰΘΕͨ߹ʹɺͰ͖ΔݶΓ ίϯύΠϥΤϥʔΛग़͢ίʔυΛॻ͖͍ͨ -
͜͜Ͱհ͞Ε͍ͯΔํ๏ͷΑ͘ͳ͍ͱ͜Ζ w 4FHVFͰભҠΛߦ͏߹ɺ֤ॴͰґଘΦϒδΣΫτ Λ͢ඞཁ͕͋Δ w %3:ʹͳΓʹ͍͘ w ඞཁͳΦϒδΣΫτΛ͞ͳͯ͘ίϯύΠϥ͕ ΤϥʔΛग़ͯ͘͠Εͳ͍ w
ˠҙਤ͠ͳ͍༻్ͰΘΕͨ߹ʹɺͰ͖ΔݶΓ ίϯύΠϥΤϥʔΛग़͢ίʔυΛॻ͖͍ͨ -
ܦҢ w ϝϧΧϦΞοςͷ։ൃΛଓ͚ΔதͰɺʮ4FHVFΛΊΑ ͏ʯͱ͍͏ྲྀΕ͕Ͱ͖ͨ w ίʔυΛॻ͖ଓ͚͍ͯ͘தͰݟ͚ͭͨ w ϝϧΧϦΞοςͰը໘ͷભҠʹ4FHVFΛ͍ͬͯͨ w ެ։ͨ͘͠ͳ͍ϓϩύςΟΛެ։͢Δඞཁ͕͋ͬͨ
w ػೳ͕૿͍͑ͯ͘ͱͱʹԿΛઃఆ͠ͳ͚Ε͍͚ͳ ͍ͷ͔͕͔Βͳ͘ͳΓෳࡶʹͳ͍ͬͯͬͨ -
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 } } ґଘ͢ΔΦϒδΣΫτͷఆٛ -
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ϝιουͰੜ͢Δ -
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { switch segue.destination
{ case let vc as WebViewController: vc.url = url default: break } } VSMΛ֎෦ʹެ։͢Δඞཁ͕͋Δ ͜ͷ7JFX$POUSPMMFSΛݺͼग़͢ॴ͕૿͑ͨ߹ɺͦͷ͜ͷίʔυΛ ॻ͘ඞཁ͕͋Δ -
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:ݪଇΛकΕΔ -
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Λͬͨํ͔ࣜͳΓ໘ ͳ͜ͱʹͳΔ -
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ϝ ιουͰ݁ͤ͞Δ͜ͱ͕Ͱ͖Δ -
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ΛͬͯભҠ࣌ͷίʔυΛநԽ -
push(viewControllerType: WebViewController.self, dependency: url) present(viewControllerType: WebViewController.self, dependency: url, withNavigationType: CustomNavigationController.self)
%FQFOEFODZ*OKFDUBCMFϓϩτίϧͷநԽͷ͓ӄͰɺ ͜Μͳײ͡Ͱָʹॻ͚ΔΑ͏ʹͳͬͨʂ -
push(viewControllerType: WebViewController.self, dependency: url) present(viewControllerType: WebViewController.self, dependency: url, withNavigationType: CustomNavigationController.self)
͍͖ͳΓ͜͜ʹͨͲΓண͍ͨΘ͚Ͱͳ͘ɺϦϑΝΫλϦϯάΛ ܁Γฦ͍ͯ͘͠தͰ͍·ͷܗʹͳ͍ͬͯͬͨ -
ྑ͍ͱ͜Ζ w ΠϯελϯεԽͷํ๏͕౷Ұ͞ΕΔ w ౷Ұ͞ΕΔ͜ͱʹΑͬͯ%3:ʹ w ࠶ར༻ָ͕ʹͳͬͨ w ґଘੑ͕ͳ͍6*7JFX$POUSPMMFSͰ %FQFOEFODZ*OKFDUBCMFʹ͓ͯ͘͜͠ͱʹΑͬ
ͯɺޙ͔Βґଘੑ͕Ճ͞Εͨͱ͖ʹαΫοͱର Ԡ͢Δ͜ͱ͕Ͱ͖ΔΑ͏ʹͳͬͨ