Kotlin Dagger 2无法提供Interactor

时间:2017-05-23 12:52:43

标签: kotlin dagger-2

我开始使用Dagger 2并遇到了奇怪的问题。 我有4个模块,其中3个在ApplicationComponent中,另​​一个具有不同的Scope(UsersScope)。

UsersInteractor注入UsersPresenter

的问题
  

错误:[BlankFragment]]如果没有@Inject构造函数或@ Provide-annotated方法,则无法提供com.interactors.UsersInteractor。

这是我的课程

@Singleton
@Component(modules = arrayOf(ApplicationModule::class, NetworkModule::class, DbModule::class))
interface ApplicationComponent {
    fun plusUsersComponent(usersModule: UsersModule): UsersComponent
}


@Module
class ApplicationModule(private val context: Context) {
@Provides
@Singleton
    fun provideContext(): Context = context
}

@Subcomponent(modules = arrayOf(UsersModule::class))
@UsersScope
interface UsersComponent {
    fun inject(blankFragment: BlankFragment)
}

@Module
class UsersModule {

@Provides
@UsersScope
fun provideUsersRepository(restService: RestService, dbService: DbService): IUsersRepository =
    UsersRepository(restService, dbService)

@Provides
@UsersScope
fun provideUsersInteractor(usersRepository: UsersRepository): IUsersInteractor =
    UsersInteractor(usersRepository)

@Provides
@UsersScope
fun provideUsersPresenter(usersInteractor: UsersInteractor): IUsersPresenter =
    UsersPresenter(usersInteractor)
}

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class UsersScope

1 个答案:

答案 0 :(得分:2)

Dagger没有UsersInteractor知道,因为您只提供IUsersInteractor

切换您的演示者提供程序方法以使用IUsersInteractor,它应该可以正常工作

fun provideUsersPresenter(usersInteractor: IUsersInteractor)