0

我正在我的 Android 应用程序中试验 VIPER 架构。我将 Dagger 2.11 用于 DI。

我对每个 VIPER 模块的依赖项是:

  1. PresenterFragment通过ViewInputViewOutput接口链接。
  2. PresenterInteractor通过InteractorInputInteractorOutput接口链接。
  3. 其他一些依赖,我们不感兴趣。

这是我的匕首模块的样子:

Module(includes = [Module.Declarations::class])
class Module(private val viewInput: ViewInput) {

    @Module
    interface Declarations {

        @Binds
        @FragmentLevel
        fun bindViewOutput(viewOutput: Presenter): ViewOutput

        @Binds
        @FragmentLevel
        fun bindInteractorOutput(interactorOutput: Presenter): InteractorOutput

        @Binds
        @FragmentLevel
        fun bindInteractorInput(interactorInput: Interactor): InteractorInput
    }

    @Provides
    @FragmentLevel
    fun provideViewInput() = viewInput
}

现在,当我打电话给inject我时Fragment,会发生以下情况:

  1. PresenterFragment通过ViewOutput接口注入
  2. InteractorPresenter通过InteractorInput接口注入
  3. Presenter没有注入,因为类型(以防止循环依赖)。InteractorLazy
  4. 在第一次调用之后通过接口Lazy.get() Presenter注入。InteractorInteractorOutput

问题是在第 1 步和第 4 步中注入了不同的实例。Presenter我怎样才能让匕首将同一个演示者注入到Fragment和中Interactor

或者也许我需要以其他方式修复依赖循环?

4

1 回答 1

0

@FragmentLevel范围添加到Presenter.

于 2018-07-31T13:49:25.423 回答