Skip to content
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

Open
wants to merge 3 commits into
base: new-architecture
Choose a base branch
from

Conversation

mori-atsushi
Copy link
Collaborator

close #130
なぜか動きません。


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
lazy var applicationCoordinator = ApplicationCoordinator(window: self.window!)
lazy var coordinator = DependencyManager.getResolver().resolve(ApplicationCoordinator.self)!
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CoordinatorをDIするメリットがあまり思いつかなかったんだが,どういったメリットが有るんだろうか.


@IBOutlet weak var signInButton: UIBarButtonItem!
@IBOutlet weak var signUpButton: UIBarButtonItem!

override func viewDidLoad() {
super.viewDidLoad()

print(viewModel)
Copy link

Choose a reason for hiding this comment

The 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() })
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

循環参照を起こします.
下記のように変更することを推奨します.

.subscribe({ [weak self] _ in self?.viewModel.onSignInClicked() })

import SwinjectStoryboard

class DependencyManager {
static let assembler = Assembler([
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

カプセル化を試みているなら,ここはprivateの方がいいかもしれないです.
また,この使用方法ならclassじゃなくてenumでも良いかも.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

また,この実装だとどこからでもDI出来てしまうが,それが実装意図に沿っているかも併せて気になりました.

@@ -9,13 +9,16 @@
import Foundation

class WelcomeViewModel {
var coordinator: WelcomeCoordinator?
let coordinator: WelcomeCoordinator
Copy link

@k-kohey k-kohey Mar 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CoordinatorがWelcomeViewControllerへの参照を持っていれば,循環参照が起きてそうだと思ったんだが,どうだろうか.
coordinatorはaqppDelegateに保持されているので,解放されることは無さそう.なので

private unowned let coordinator: WelcomeCoordinator

が良さそう.

一方で,ViewModelがCoordinatorを直接持つ必要も無さそうなので,外部から遷移ロジックを注入するのが一番安全かも.

class ViewControllerAssembly: Assembly {
func assemble(container: Container) {
container.storyboardInitCompleted(WelcomeViewConstoller.self) { res, viewController in
viewController.viewModel = res.resolve(WelcomeViewModel.self)!
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

抽象に依存してないが,これは実装意図に沿っているだろうか.
また,ここでランタイムエラーが起きないかも併せて気になりました.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DI Containerを作成
2 participants