-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] DI containerの設定 #138
base: new-architecture
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// CoordinatorAssembly.swift | ||
// SyncPod | ||
// | ||
// Created by 森篤史 on 2018/05/20. | ||
// Copyright © 2018年 Cyder. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import Swinject | ||
|
||
class CoordinatorAssembly: Assembly { | ||
func assemble(container: Container) { | ||
container.register(ApplicationCoordinator.self) { res in | ||
ApplicationCoordinator( | ||
welcomeCoordinator: res.resolve(WelcomeCoordinator.self)! | ||
) | ||
}.inObjectScope(.container) | ||
|
||
container.register(WelcomeCoordinator.self) { _ in | ||
WelcomeCoordinator() | ||
}.inObjectScope(.container) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// DependencyManager.swift | ||
// SyncPod | ||
// | ||
// Created by 森篤史 on 2018/05/21. | ||
// Copyright © 2018年 Cyder. All rights reserved. | ||
// | ||
|
||
import Swinject | ||
import SwinjectStoryboard | ||
|
||
class DependencyManager { | ||
static let assembler = Assembler([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. カプセル化を試みているなら,ここはprivateの方がいいかもしれないです. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. また,この実装だとどこからでもDI出来てしまうが,それが実装意図に沿っているかも併せて気になりました. |
||
CoordinatorAssembly(), | ||
ViewModelAssembly(), | ||
ViewControllerAssembly() | ||
], container: SwinjectStoryboard.defaultContainer) | ||
|
||
static func getResolver() -> Resolver { | ||
return assembler.resolver | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// ViewControllerAssembly.swift | ||
// SyncPod | ||
// | ||
// Created by 森篤史 on 2018/05/20. | ||
// Copyright © 2018年 Cyder. All rights reserved. | ||
// | ||
|
||
import Swinject | ||
import SwinjectStoryboard | ||
|
||
class ViewControllerAssembly: Assembly { | ||
func assemble(container: Container) { | ||
container.storyboardInitCompleted(WelcomeViewConstoller.self) { res, viewController in | ||
viewController.viewModel = res.resolve(WelcomeViewModel.self)! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 抽象に依存してないが,これは実装意図に沿っているだろうか. |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// ViewModelAssembly.swift | ||
// SyncPod | ||
// | ||
// Created by 森篤史 on 2018/05/20. | ||
// Copyright © 2018年 Cyder. All rights reserved. | ||
// | ||
|
||
import Swinject | ||
|
||
class ViewModelAssembly: Assembly { | ||
func assemble(container: Container) { | ||
container.register(WelcomeViewModel.self) { res in | ||
WelcomeViewModel( | ||
coordinator: res.resolve(WelcomeCoordinator.self)! | ||
) | ||
}.inObjectScope(.container) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,23 +12,25 @@ import RxCocoa | |
|
||
class WelcomeViewConstoller: UIViewController { | ||
let disposeBag = DisposeBag() | ||
var viewModel: WelcomeViewModel? | ||
var viewModel: WelcomeViewModel! | ||
|
||
@IBOutlet weak var signInButton: UIBarButtonItem! | ||
@IBOutlet weak var signUpButton: UIBarButtonItem! | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
print(viewModel) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. デバッグ時に使用していた記述が残っています. |
||
|
||
setUpDataBinding() | ||
} | ||
|
||
private func setUpDataBinding() { | ||
signInButton.rx.tap | ||
.subscribe({ _ in self.viewModel?.onSignInClicked() }) | ||
.subscribe({ _ in self.viewModel.onSignInClicked() }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 循環参照を起こします.
|
||
.disposed(by: disposeBag) | ||
signUpButton.rx.tap | ||
.subscribe({ _ in self.viewModel?.onSignUpClicked() }) | ||
.subscribe({ _ in self.viewModel.onSignUpClicked() }) | ||
.disposed(by: disposeBag) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,16 @@ | |
import Foundation | ||
|
||
class WelcomeViewModel { | ||
var coordinator: WelcomeCoordinator? | ||
let coordinator: WelcomeCoordinator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CoordinatorがWelcomeViewControllerへの参照を持っていれば,循環参照が起きてそうだと思ったんだが,どうだろうか.
が良さそう. 一方で,ViewModelがCoordinatorを直接持つ必要も無さそうなので,外部から遷移ロジックを注入するのが一番安全かも. |
||
|
||
init(coordinator: WelcomeCoordinator) { | ||
self.coordinator = coordinator | ||
} | ||
func onSignInClicked() { | ||
coordinator?.navigateToSignIn() | ||
coordinator.navigateToSignIn() | ||
} | ||
|
||
func onSignUpClicked() { | ||
coordinator?.navigateToSignUp() | ||
coordinator.navigateToSignUp() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CoordinatorをDIするメリットがあまり思いつかなかったんだが,どういったメリットが有るんだろうか.