gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:21:14 33 4
gpt4 key购买 nike

我正在尝试使用 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com