我有一个接口WordsDataSource
,我使用它实现了两个具体的类,即WordsLocalDataSource
处理本地数据库和另一个WordsRemoteDataSource
处理在服务器上在线处理数据。问题是当我尝试使用抽象类名将两个类注入存储库类WordsDataSource
时
DefaultWordsRepository(
private val wordsRemoteDataSource: WordsDataSource,
private val wordsLocalDataSource: WordsDataSource) {
并在 Application 类中添加依赖项,例如
class WordsApplication : Application(), KodeinAware {
override val kodein = Kodein.lazy {
import(androidXModule(this@WordsApplication))
bind() from singleton { WordsDatabase.getInstance(instance()) }
bind<WordsDao>() with singleton { instance<WordsDatabase>().wordsDao() }
bind() from singleton { WordsLocalDataSource(instance()) }
bind() from singleton { WordsRemoteDataSource() }
bind<WordsRepository>() with singleton { DefaultWordsRepository(instance(), instance()) }
bind() from provider { ViewModelFactory(instance()) }
}
然后在运行应用程序时,我在 logcat 中遇到以下问题
org.kodein.di.Kodein$NotFoundException: 2 bindings found that match bind<WordsDataSource>() with ?<WordsFragment>().? { ? }:
bind<WordsLocalDataSource>() with singleton { WordsLocalDataSource }
bind<WordsRemoteDataSource>() with singleton { WordsRemoteDataSource }
我已经通过简单地通过它们各自的具体类名声明变量来尝试解决此问题,例如
DefaultWordsRepository(
private val wordsRemoteDataSource: WordsRemoteDataSource,
private val wordsLocalDataSource: WordsLocalDataSource) {
但是还是想知道有没有办法解决这个问题。
我正在为 kodein 使用以下依赖项
implementation "org.kodein.di:kodein-di-generic-jvm:6.3.3"
implementation "org.kodein.di:kodein-di-framework-android-x:6.3.3"