我对Dagger还是很陌生,当我想注入接口实现时遇到一些错误,我有一个名为Mapper<K,V>
的接口,它的实现是ArticleEntityArticleMapper
error: [Dagger/MissingBinding]ir.siatech.newsappkotlinclean.domain.commons.Mapper<? superir.siatech.newsappkotlinclean.domain.entities.ArticleEntity,ir.siatech.newsappkotlinclean.presentation.entities.Article> cannot be provided without an @Provides-annotated method.
这是我的代码:
@Module
abstract class MapperModule {
@Binds
abstract fun providesArticleEntityArticleMapper(articleEntityArticleMapper: ArticleEntityArticleMapper)
: Mapper<ArticleEntity, Article>
}
@Module(includes = [ViewModelModule::class])
abstract class ViewModelBuilder {
@Binds
abstract fun bindViewModelFactory(viewModelFactory: ViewModelFactory): ViewModelProvider.Factory
}
@Module
abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(NewsViewModel::class)
abstract fun bindNewsViewModel(newsViewModel: NewsViewModel): ViewModel
}
这是我的ViewModel:
class NewsViewModel @Inject constructor() : BaseViewModel() {
@Inject
lateinit var articleEntityArticleMapper: Mapper<ArticleEntity, Article>
}
编辑:
@Singleton
@Component(
modules = [
AndroidSupportInjectionModule::class,
UseCaseModule::class,
MapperModule::class,
DataModule::class,
ActivityBuilder::class,
ContextModule::class,
NetworkModule::class]
)
interface AppComponent : AndroidInjector<NewsApp> {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun build(): AppComponent
}
}