我开始使用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
答案 0 :(得分:2)
Dagger没有UsersInteractor
知道,因为您只提供IUsersInteractor
。
切换您的演示者提供程序方法以使用IUsersInteractor
,它应该可以正常工作
fun provideUsersPresenter(usersInteractor: IUsersInteractor)