我目前正在开发一款新闻馈送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 :)