在干净的体系结构中使用资源包装器与多个实体和映射类别类别

发布于 2025-01-29 11:18:28 字数 1200 浏览 5 评论 0原文

假设我们有一个应用程序,该应用将应用清洁的体系结构原则。 我们有存储库,数据源(本地&远程),每个层的多个值对象和用于映射这些对象的映射类。

还有一个称为资源的包装类别(类似于这篇文章< /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?
    }
}

现在我的问题是:

  1. 不同实体之间的映射如何以及何处发生?如果它发生在存储库中,则应该如何实现(因为我们将对象包装在资源中)?

  2. 我正在从数据源中暴露资源,以便捕获其中的任何例外,并且仅以通用格式发送最终结果,这是一个好习惯吗?如果不是,那么在哪里处理错误的地方在哪里?

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:

  1. 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)?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文