我正在尝试使用 Koin 制作一个 Android 玩具项目。
我的项目有一个存储库和两个数据源(远程/缓存)。
这里是数据源:
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 = module {
single<DataSource>(StringQualifier("cache")) { CacheDataSource() }
single<DataSource>(StringQualifier("remote")) { RemoteDataSource() }
single<MyRepository> { MyRepositoryImpl(get<MoviesDataSource>(name = "cache"), get<MoviesDataSource>(name = "remote")) }
}
但我不知道该怎么做?
最佳答案
我找到了解决方案...
val appModule = module {
single<DataSource>(StringQualifier("cache")) { CacheDataSource() }
single<DataSource>(StringQualifier("remote")) { RemoteDataSource() }
single<MyRepository> { MyRepositoryImpl(get(StringQualifier("cache")), get(StringQualifier("remote"))) }
}
关于Android Koin,如何使用两个数据源制作存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56062685/