2017-03-27 93 views
3

在我的項目中的這一點上,我使用Swinject(DI容器)。Swinject註冊UIViewController最佳實踐

我想知道如何註冊的ViewController,如果我有以下層次:

class RateAnswersBaseVC: UIViewController { } 
class RateAnswersDoctorVC: RateAnswersBaseVC { } 
class RateAnswersQualityVC: RateAnswersBaseVC { } 

如何將他們的權利,註冊和使用?我在我使用的第一個選項的項目這一點兩個選項,但有一種感覺,我們應該使用第二個:)

// ------- 1 -------- 
container.register(RateAnswersDoctorVC.self) { r in 
    let vc: RateAnswersDoctorVC = RateAnswersDoctorVC() 
    self.configureBasicFields(with: vc, container: container, resolver: r) 
    return vc 
} 

container.register(RateAnswersQualityVC.self) { r in 
    let vc: RateAnswersQualityVC = RateAnswersQualityVC() 
    self.configureBasicFields(with: vc, container: container, resolver: r) 
    return vc 
} 

// ------- 2 -------- 
container.register(RateAnswersBaseVC.self, name: "doctor") { r in 
    let vc: RateAnswersDoctorVC = RateAnswersDoctorVC() 
    self.configureBasicFields(with: vc, container: container, resolver: r) 
    return vc 
} 

container.register(RateAnswersBaseVC.self, name: "quality") { r in 
    let vc: RateAnswersQualityVC = RateAnswersQualityVC() 
    self.configureBasicFields(with: vc, container: container, resolver: r) 
    return vc 
} 

如果你解釋爲什麼你使用一個或另一個我會很高興方法。

感謝您的答覆

回答

0

UMH,無論是1或2,你還在註冊實例。 在這種情況下我沒有看到任何區別。

這些ViewControllers這樣的事情是一個比較複雜一點,但什麼會有所作爲。將不會登記他們作爲RateAnswersBaseVC,但作爲一個協議RateAnswersBaseVCServiceRateAnswersBaseVCProtocol,這樣你可以再創建一個不同的實現視圖控制器在模擬測試。

0

上次在我的項目中使用MVVM模式。因此,我將應用程序的業務邏輯分爲模塊-提供了一些功能或業務邏輯(如在應用程序中登錄,以全屏方式播放視頻等)的完整代碼部分。對於每個模塊,我創建一個Assembly對象來配置模塊中的依賴關係。

class PasswordAssembly: Assembly { 
    func assemble(container: Container) { 
     container.register(PasswordViewInterface.self) { (_: Resolver) in 
      PasswordViewController(nibName: "PasswordViewController", bundle: nil) 
     } 
     .initCompleted { resolver, view in 
      var view = view as PasswordViewInterface 
      view.viewModel = resolver.resolve(PasswordViewModelInterface.self) 
      view.router = resolver.resolve(PasswordRouterInterface.self) 
     } 

     container.register(PasswordViewModelInterface.self) { (resolver: Resolver) in 
      return PasswordViewModel(coreModel: resolver.resolve(CoreViewModelInterface.self)!) 
     } 

     container.register(PasswordRouterInterface.self) { _ in 
      return PasswordRouter() 
     } 
     .initCompleted { (resolver, router) in 
      var router = router as PasswordRouterInterface 
      router.view = resolver.resolve(PasswordViewInterface.self) 
      router.resolver = container 
     } 
    } 
} 

而且我在路由器注入Resolver(容器)對象,使Router構建層次

protocol PasswordRouterInterface { 
    var view: PasswordViewInterface! { get set } 
    var resolver: Resolver! { get set } 

    func presentFilesListView() 
    func presentContentView() 
} 

class PasswordRouter: PasswordRouterInterface { 
    var view: PasswordViewInterface! 
    var resolver: Resolver! 

    func presentFilesListView() { 
     if let listViewController = resolver.resolve(FilesListViewInterface.self)?.getViewController() { 
      let navigationController = UINavigationController(rootViewController: listViewController) 
      view.getViewController().present(navigationController, animated: true) { 

      } 
     } 
    } 

    func presentContentView() { 

    } 
}