Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Koin,如何使用两个数据源创建存储库?_Android_Kotlin_Koin - Fatal编程技术网

Android Koin,如何使用两个数据源创建存储库?

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

我正在尝试使用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=模块{
单个(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一样,不可能有多个。