我目前正在开发一款新闻馈送Android应用程序.我试着按照干净架构的原则来设计我的应用程序.

在数据层,我使用repository模式作为不同数据源的门面:来自API(https://newsapi.org/)的远程数据、来自DB(Realm或SQLite)的本地数据以及一些内存缓存

Does it make sense to use different model classes for the remote data source as well as for the local data source?

E.g. The remote data source uses Retrofit to make API calls and the models need to be annotated in order to be parsed by GSON.

data class RemoteArticleModel(
        @SerializedName("title") val title: String,
        @SerializedName("urlToImage") val urlToImage: String,
        @SerializedName("url") val url: String)

The models for the local data source also may have to fulfill some certain contract like models in a Realm DB need to extend RealmObject.

open class Dog : RealmObject() {
    var name: String? = null
    @LinkingObjects("dog")
    val owners: RealmResults<Person>? = null
}

Obviously, I don´t want my domain models to be 'polluted' by any data source specific contract (annotations, RealmObject inheritance, etc.). So I thought it would make sense to use different models for different data sources and the repository handles the mapping between them.

E.g. We want to fetch all articles from the remote API, store them in the local DB and return them to the domain layer.

Flow would be like: Remote data source makes http request to news api and retrieves a list of RemoteArticleModel´s. The repository would map these models to a Domain specific article model (Article). Then these would be mapped to DB models (e.g. RealmArticleModel) and inserted into the DB. Finally the list of Article´s would be returned to the caller.

Two questions arise:个 上面的例子显示了many allocations将如何使用此方法. 对于要下载并插入到DB中的每一篇文章,将在该过程中创建三个模型.那会不会是矫枉过正呢?

此外,我知道数据层应该使用与域层不同的模型类(内层不应该与外层无关).但在上面的例子中,这有什么意义呢.对于这两个不同的数据源,我已经有了两个不同的模型类.添加第三个被数据层/存储库用作"中介"模型的模型,以处理到其他模型(远程、本地、域)的映射,将增加更多的分配.

So should the data layer know nothing about domain models and let the domain do the mapping from a data layer model to a domain layer model?

Should there be a generic model used only by the repository/data-layer?

Thank, I really appreciate any help from more experienced developers :)

推荐答案

The overriding principle you should follow is separation of concerns.

The persistence layer should have classes that only deal with the storing and retrieval of data, in this case the Realm classes.

The network layer should have classes that deal with the data from the server, in this case the Retrofit classes.

将数据从这些层移动到业务层需要将持久性和网络对象映射到域.

为了回答您的第一个问题,映射隔离了不同的关注点,将域与数据层分开.数据层不应该知道域模型.域从数据层请求数据,数据层获取数据并通过映射器将其传递,从而返回域模型.

为了回答你的第二个问题,如果你从不同的来源获得数据,那么为你的数据层建立一个通用的模型就违反了关注点分离原则.持久性模型和网络模型代表系统的不同部分,因此应该用不同的模型来表示.域不需要知道这一点,因此,在跨越边界返回域之前,请求的数据应该映射到域对象.

Kotlin相关问答推荐

带有Spring Boot和Kotline的可嵌入实体

何时使用figureEach

为什么onEach不是挂起函数,而Collect是?

数据源配置

在Kotlin lambda的参数中如何指定函数类型?

Kotlin 复制列表中的项目以创建具有相同数据的不同对象的新列表

Criteria Api 中的 Kotlin 泛型

Kotlin - 协程未按预期执行

奇怪的 cotlin check Not Null 参数错误

为什么 Kotlin 扩展运算符在传递原始可变参数时需要 toTypedArray()?

如何禁用智能投射突出显示 Kotlin?

Hilt Activity 必须附加到 @AndroidEntryPoint 应用程序

作为 Kotlin 中的函数的结果,如何从 Firestore 数据库返回列表?

主机名不能为空

如何在Kotlin中创建填充空值的通用数组?

Android Jetpack导航,另一个主机片段中的主机片段

Mocked suspend函数在Mockito中返回null

如何根据ArrayList的对象属性值从中获取最小/最大值?

具有多个 parameter的 Kotlin 枚举

java.lang.NoClassDefFoundError:解析失败:Lkotlin/time/MonoClock