Android Koin,如何使用两个数据源创建存储库?
我正在尝试使用Koin制作一个Android玩具项目 我的项目有一个存储库和两个数据源(远程/缓存) 以下是数据源:Android Koin,如何使用两个数据源创建存储库?,android,kotlin,koin,Android,Kotlin,Koin,我正在尝试使用Koin制作一个Android玩具项目 我的项目有一个存储库和两个数据源(远程/缓存) 以下是数据源: interface DataSource class CacheDataSource: DataSource class RemoteDataSource: DataSource 以下是存储库: interface MyRepository class MyRepositoryImpl( val cacheDataSource: DataSource, v
interface DataSource
class CacheDataSource: DataSource
class RemoteDataSource: DataSource
以下是存储库:
interface MyRepository
class MyRepositoryImpl(
val cacheDataSource: DataSource,
val remoteDataSource: DataSource
): MyRepository
所以。。。我正在编写appModule代码,如下所示:
val appModule = module {
single<DataSource>(StringQualifier("cache")) { CacheDataSource() }
single<DataSource>(StringQualifier("remote")) { RemoteDataSource() }
single<MyRepository> { MyRepositoryImpl() as MyRepository by inject("???") }
}
val appModule=模块{
单个(StringQualifier(“cache”){CacheDataSource()}
单个(StringQualifier(“remote”){RemoteDataSource()}
单个{MyRepositoryImpl()作为MyRepository by inject(“???”)}
}
而且
我也试过下面的代码…:
val appModule = module {
single<DataSource>(StringQualifier("cache")) { CacheDataSource() }
single<DataSource>(StringQualifier("remote")) { RemoteDataSource() }
single<MyRepository> { MyRepositoryImpl(get<MoviesDataSource>(name = "cache"), get<MoviesDataSource>(name = "remote")) }
}
val appModule=模块{
单个(StringQualifier(“cache”){CacheDataSource()}
单个(StringQualifier(“remote”){RemoteDataSource()}
单个{MyRepositoryImpl(get(name=“cache”)、get(name=“remote”)}
}
但我不知道该怎么做?我找到了解决方案
val appModule = module {
single<DataSource>(StringQualifier("cache")) { CacheDataSource() }
single<DataSource>(StringQualifier("remote")) { RemoteDataSource() }
single<MyRepository> { MyRepositoryImpl(get(StringQualifier("cache")), get(StringQualifier("remote"))) }
}
val appModule=模块{
单个(StringQualifier(“cache”){CacheDataSource()}
单个(StringQualifier(“remote”){RemoteDataSource()}
单个{MyRepositoryImpl(get(StringQualifier(“缓存”)),get(StringQualifier(“远程”))}
}
您也可以这样做:
val appModule = module {
single<CacheDataSource> { CacheDataSource() }
single<RemoteDataSource> { RemoteDataSource() }
single<MyRepository> { MyRepositoryImpl(get(), get()) }
}
val appModule=模块{
单个{CacheDataSource()}
单个{RemoteDataSource()}
单个{MyRepositoryImpl(get(),get())}
}
如何将许多数据源分组到“假”和“真”限定符下?我犯了一个错误,因为限定符就像ID一样,不可能有多个。