在干净的体系结构中使用资源包装器与多个实体和映射类别类别
假设我们有一个应用程序,该应用将应用清洁的体系结构原则。 我们有存储库,数据源(本地&远程),每个层的多个值对象和用于映射这些对象的映射类。
还有一个称为资源的包装类别(类似于这篇文章< /a>)。
例如:
interface PriceRepository {
suspend fun getPrices(): Resource<PriceData>
}
interface PriceRemoteDataSource {
suspend fun getPrices(): Resource<RemotePriceData>
}
interface PriceLocalDataSource {
suspend fun getPrices(): Resource<PriceDataEntity>
}
interface Mapper<I, O> {
fun mapTo(input: I): O
}
class PriceRepositoryImpl: PriceRepository {
override suspend fun getPrices() {
val localPrices: Resource<PriceDataEntity> = localDataSource.getPrices()
val remotePrices: Resource<RemotePriceData> = remoteDataSource.getPrices()
// Some logic ...
// Map results to Resource<PriceData> and return them? how?
}
}
现在我的问题是:
不同实体之间的映射如何以及何处发生?如果它发生在存储库中,则应该如何实现(因为我们将对象包装在资源中)?
我正在从数据源中暴露资源,以便捕获其中的任何例外,并且仅以通用格式发送最终结果,这是一个好习惯吗?如果不是,那么在哪里处理错误的地方在哪里?
Let's say we have an app that is going to apply clean architecture principles.
we have repositories, data sources(local & remote), multiple value objects for each layer and mapper classes for mapping those objects.
There is also a wrapper class called Resource (similar to what is discussed in this post).
for example:
interface PriceRepository {
suspend fun getPrices(): Resource<PriceData>
}
interface PriceRemoteDataSource {
suspend fun getPrices(): Resource<RemotePriceData>
}
interface PriceLocalDataSource {
suspend fun getPrices(): Resource<PriceDataEntity>
}
interface Mapper<I, O> {
fun mapTo(input: I): O
}
class PriceRepositoryImpl: PriceRepository {
override suspend fun getPrices() {
val localPrices: Resource<PriceDataEntity> = localDataSource.getPrices()
val remotePrices: Resource<RemotePriceData> = remoteDataSource.getPrices()
// Some logic ...
// Map results to Resource<PriceData> and return them? how?
}
}
Now my questions are:
How and where the mapping between different entities should happen? if it happens inside the repository, how should it be implemented (since we are wrapping objects inside Resource)?
I am exposing Resource from data sources in order to catch any exceptions inside them and only send the final result in a common format, is this a good practice? if not, where is the better place to handle errors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论