干净的架构 Android
Clean Architecture Android
我正在尝试了解简洁的架构。我对这篇文章question.According
Making a network request is one of the most common tasks an Android app might perform. The News app needs to present the user with the latest news that is fetched from the network. Therefore, the app needs a data source class to manage network operations: NewsRemoteDataSource.To expose the information to the rest of the app, a new repository that handles operations on news data is created: NewsRepository.
https://developer.android.com/jetpack/guide/data-layer#create_the_data_source
我发现了很多像下面这样的例子。为什么他们在存储库 class 中管理网络操作。 Android 说我们应该使用数据源 Class。您能解释一下需要在存储库 class 中完成哪些工作吗?也许有一些例子。
class WordInfoRepositoryImpl(
private val api: DictionaryApi,
private val dao: WordInfoDao
): WordInfoRepository {
override fun getWordInfo(word: String): Flow<Resource<List<WordInfo>>> = flow {
emit(Resource.Loading())
val wordInfos = dao.getWordInfos(word).map { it.toWordInfo() }
emit(Resource.Loading(data = wordInfos))
try {
val remoteWordInfos = api.getWordInfo(word)
dao.deleteWordInfos(remoteWordInfos.map { it.word })
dao.insertWordInfos(remoteWordInfos.map { it.toWordInfoEntity() })
} catch(e: HttpException) {
emit(Resource.Error(
message = "Oops, something went wrong!",
data = wordInfos
))
} catch(e: IOException) {
emit(Resource.Error(
message = "Couldn't reach server, check your internet connection.",
data = wordInfos
))
}
val newWordInfos = dao.getWordInfos(word).map { it.toWordInfo() }
emit(Resource.Success(newWordInfos))
}
}
这是一篇很好的文章,解释了 DAO
和 Repository
之间的区别。 https://www.baeldung.com/java-dao-vs-repository
我认为 API
是远程数据库的临时 DAO
。
由于您无权访问远程数据库结构,因此这将是您最接近 DAO
.
的事情
这个Repository
负责整理两个数据源。 DAO
是本地的,API
是远程的。
看起来它检索了本地数据并发出了它。然后它会尝试检索远程数据以更新本地数据。它最终然后 re-emits 本地数据。
根据文章,我认为您发布的示例属于 Repository
的正常职责。
我正在尝试了解简洁的架构。我对这篇文章question.According
Making a network request is one of the most common tasks an Android app might perform. The News app needs to present the user with the latest news that is fetched from the network. Therefore, the app needs a data source class to manage network operations: NewsRemoteDataSource.To expose the information to the rest of the app, a new repository that handles operations on news data is created: NewsRepository.
https://developer.android.com/jetpack/guide/data-layer#create_the_data_source
我发现了很多像下面这样的例子。为什么他们在存储库 class 中管理网络操作。 Android 说我们应该使用数据源 Class。您能解释一下需要在存储库 class 中完成哪些工作吗?也许有一些例子。
class WordInfoRepositoryImpl(
private val api: DictionaryApi,
private val dao: WordInfoDao
): WordInfoRepository {
override fun getWordInfo(word: String): Flow<Resource<List<WordInfo>>> = flow {
emit(Resource.Loading())
val wordInfos = dao.getWordInfos(word).map { it.toWordInfo() }
emit(Resource.Loading(data = wordInfos))
try {
val remoteWordInfos = api.getWordInfo(word)
dao.deleteWordInfos(remoteWordInfos.map { it.word })
dao.insertWordInfos(remoteWordInfos.map { it.toWordInfoEntity() })
} catch(e: HttpException) {
emit(Resource.Error(
message = "Oops, something went wrong!",
data = wordInfos
))
} catch(e: IOException) {
emit(Resource.Error(
message = "Couldn't reach server, check your internet connection.",
data = wordInfos
))
}
val newWordInfos = dao.getWordInfos(word).map { it.toWordInfo() }
emit(Resource.Success(newWordInfos))
}
}
这是一篇很好的文章,解释了 DAO
和 Repository
之间的区别。 https://www.baeldung.com/java-dao-vs-repository
我认为 API
是远程数据库的临时 DAO
。
由于您无权访问远程数据库结构,因此这将是您最接近 DAO
.
这个Repository
负责整理两个数据源。 DAO
是本地的,API
是远程的。
看起来它检索了本地数据并发出了它。然后它会尝试检索远程数据以更新本地数据。它最终然后 re-emits 本地数据。
根据文章,我认为您发布的示例属于 Repository
的正常职责。