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

레거시 프로젝트에서 의존성 주입하기

Suyeol Jeon
December 05, 2019

레거시 프로젝트에서 의존성 주입하기

왜 개념만 알려주고 적용 방법은 아무도 알려주지 않는가

Suyeol Jeon

December 05, 2019
Tweet

More Decks by Suyeol Jeon

Other Decks in Programming

Transcript

  1. func fetchUser() -> Single<User> { return Networking.shared.get("/user/1") } ъೠ ੄ઓ

    ডೠ ੄ઓ Networking NetworkingStub Production Testing
  2. func fetchUser() -> Single<User> { return self.networking.get("/user/1") } ъೠ ੄ઓ

    ডೠ ੄ઓ Networking NetworkingStub Production Testing
  3. struct AppDependency { let window: UIWindow } extension AppDependency {

    static func resolve() -> AppDependency { } } CompositionRoot
  4. struct AppDependency { let window: UIWindow } extension AppDependency {

    static func resolve() -> AppDependency { return AppDependency( ) } } CompositionRoot
  5. struct AppDependency { let window: UIWindow } extension AppDependency {

    static func resolve() -> AppDependency { return AppDependency( window: ... ) } } CompositionRoot
  6. struct AppDependency { let window: UIWindow } extension AppDependency {

    } CompositionRoot static func resolve() -> AppDependency { return AppDependency( window: ... ) }
  7. let networking = Networking() let window = UIWindow() window.rootViewController =

    ListVC( userService: UserService( networking: networking ), detailVCFactory: { DetailVC( repoService: RepoService( networking: networking ) ) } ) return AppDependency( window: window )
  8. let networking = Networking() let window = UIWindow() window.rootViewController =

    ListVC( userService: UserService( networking: networking ), detailVCFactory: { DetailVC( repoService: RepoService( networking: networking ) ) } ) return AppDependency( window: window )
  9. let networking = Networking() let window = UIWindow() window.rootViewController =

    ListVC( userService: UserService( networking: networking ), detailVCFactory: { DetailVC( repoService: RepoService( networking: networking ) ) } ) return AppDependency( window: window )
  10. let networking = Networking() let window = UIWindow() window.rootViewController =

    ListVC( userService: UserService( networking: networking ), detailVCFactory: { DetailVC( repoService: RepoService( networking: networking ) ) } ) return AppDependency( window: window )
  11. let networking = Networking() let window = UIWindow() window.rootViewController =

    ListVC( userService: UserService( networking: networking ), detailVCFactory: { DetailVC( repoService: RepoService( networking: networking ) ) } ) return AppDependency( window: window )
  12. let networking = Networking() let window = UIWindow() window.rootViewController =

    ListVC( userService: UserService( networking: networking ), detailVCFactory: { DetailVC( repoService: RepoService( networking: networking ) ) } ) return AppDependency( window: window )
  13. let networking = Networking() let window = UIWindow() window.rootViewController =

    ListVC( userService: UserService( networking: networking ), detailVCFactory: { DetailVC( repoService: RepoService( networking: networking ) ) } ) return AppDependency( window: window )
  14. let networking = Networking() let window = UIWindow() window.rootViewController =

    ListVC( userService: UserService( networking: networking ), detailVCFactory: { DetailVC( repoService: RepoService( networking: networking ) ) } ) return AppDependency( window: window )
  15. class AppDelegate { var window: UIWindow? func application( _ application:

    UIApplication, didFinishLaunchingWithOptions ... ) -> Bool { self.window = UIWindow(...) self.window?.makeKeyAndVisible() return true } }
  16. class AppDelegate { let dependency: AppDependency var window: UIWindow? func

    application( _ application: UIApplication, didFinishLaunchingWithOptions ... ) -> Bool { self.window = UIWindow(...) self.window?.makeKeyAndVisible() return true } }
  17. class AppDelegate { let dependency: AppDependency var window: UIWindow? private

    override init() { } func application( _ application: UIApplication, didFinishLaunchingWithOptions ... ) -> Bool { self.window = UIWindow(...) self.window?.makeKeyAndVisible() return true } }
  18. class AppDelegate { let dependency: AppDependency var window: UIWindow? private

    override init() { self.dependency = AppDependency.resolve() } func application( _ application: UIApplication, didFinishLaunchingWithOptions ... ) -> Bool { self.window = UIWindow(...) self.window?.makeKeyAndVisible() return true } }
  19. class AppDelegate { let dependency: AppDependency var window: UIWindow? private

    override init() { self.dependency = AppDependency.resolve() } func application( _ application: UIApplication, didFinishLaunchingWithOptions ... ) -> Bool { self.window = self.dependency.window self.window?.makeKeyAndVisible() return true } }
  20. Composition Rootо పझ౟ ജ҃ীࢲ प೯غ૑ ঋب۾ UIApplicationMain( CommandLine.argc, CommandLine.unsafeArgv, NSStringFromClass(UIApplication.self),

    NSStringFromClass(NSClassFromString("MyTests.TestAppDelegate") ?? AppDelegate.self) ) https://github.com/devxoul/Pure
  21. ੄ઓࢿ Ӓې೐ী ࣘೞ૑ ޅೠ׮. ৵? 1. ੿੸ ੋఠಕ੉झী ੄ઓೠ׮ 2.

    ੋझఢझܳ ૒੽ ࢤࢿೠ׮ UserService.shared.fetchUser()
  22. ੄ઓࢿ Ӓې೐ী ࣘೞ૑ ޅೠ׮. ৵? 1. ੿੸ ੋఠಕ੉झী ੄ઓೠ׮ 2.

    ੋझఢझܳ ૒੽ ࢤࢿೠ׮ let detailVC = DetailVC() UserService.shared.fetchUser()
  23. 1. ੿੸ ੋఠಕ੉झী ੄ઓೠ׮ 2. ੋझఢझܳ ૒੽ ࢤࢿೠ׮ let detailVC

    = DetailVC() UserService.shared.fetchUser() ੄ઓࢿ Ӓې೐ী ࣘೞ૑ ޅೠ׮. ৵?
  24. അप੄ ݾ಴ যઁࠁ׮ ؊ ա਷ ௏٘ܳ ੘ࢿೠ׮ ੸যب ৻ࠗীࢲ ઱ੑೡ

    ࣻ ੓ѱ غ঻׮ ೙ਃ೧૕ ٸ ೐۽ష௒ਸ ٜ݅੗
  25. 2. ੋझఢझܳ ૒੽ ࢤࢿೠ׮ let detailVC = DetailVC() ࢤࢿ੗۽ ಂషܻܳ

    ੹׳߉ח׮ init(detailVCFactory: () -> DetailVC) let detailVC = self.detailVCFactory()
  26. അप੄ ݾ಴ যઁࠁ׮ ؊ ա਷ ௏٘ܳ ੘ࢿೠ׮ ੸যب ৻ࠗীࢲ ઱ੑೡ

    ࣻ ੓ѱ غ঻׮ ࠂ੟೧૑ݶ Factory ఋੑਸ ੿੄ೞ੗
  27. ف ೧Ѿ ߑߨ੄ ҕా੼ ੄ઓࢿ੉ ৻ࠗী ਤ஖ೠ׮. ೙ਃী ٮۄ ਗೞח

    ѐ୓ܳ ઱ੑೡ ࣻ ੓׮. ListVC(detailVCFactory: { DetailVCMock() }) UserVC(userService: UserServiceMock())
  28. window.rootViewController = ListVC( userService: UserService( networking: networking ), detailVCFactory: {

    DetailVC( repoService: RepoService( networking: networking ) ) } ) ղੌ+4