我正在我的 Android 应用程序中试验 VIPER 架构。我将 Dagger 2.11 用于 DI。
我对每个 VIPER 模块的依赖项是:
Presenter并Fragment通过ViewInput和ViewOutput接口链接。Presenter并Interactor通过InteractorInput和InteractorOutput接口链接。- 其他一些依赖,我们不感兴趣。
这是我的匕首模块的样子:
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,会发生以下情况:
PresenterFragment通过ViewOutput接口注入InteractorPresenter通过InteractorInput接口注入Presenter没有注入,因为类型(以防止循环依赖)。InteractorLazy- 在第一次调用之后通过接口
Lazy.get()Presenter注入。InteractorInteractorOutput
问题是在第 1 步和第 4 步中注入了不同的实例。Presenter我怎样才能让匕首将同一个演示者注入到Fragment和中Interactor?
或者也许我需要以其他方式修复依赖循环?