干净的架构:在不同的层共享相同的模型/实体 [英] Clean architecture: share same models/entities with different layers

查看:14
本文介绍了干净的架构:在不同的层共享相同的模型/实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的干净架构 Android 应用程序设置中,我为每一层(数据、域、表示)都有自己的 Gradle 模块.我也为每一层都有自己的模型/实体,它们使用映射器从一层转换到另一层.这导致我有很多 kotlin 数据类,代表基本相同的东西,但在不同的层.这对我来说不太合适.

In my clean architecture Android app setup, I have own Gradle module for each layer (data, domain, presentation). I also have own models/entities for each layers, which are converted from one layer to another using mappers. This leads to situation where I have a lot of kotlin data classes, representing basically same thing, but in different layer. This does not sound right to me.

简单例子:

数据层 - Android 库模块

Data layer - Android library module

@JsonClass(generateAdapter = true)
data class BuildingEntity(
    @Json(name = "u_id")
    val id: String,

    val name: String,

    val latitude: Double,

    val longitude: Double,

    @Json(name = "current_tenants")
    val tenants: List<TenantEntity>? = null
)

领域层 - 纯 Kotlin 模块

Domain layer - Pure Kotlin module

data class Building(

    val id: String,

    val name: String,

    val location: CoordinatePoint,

    val tenants: List<Tenant>? = null

展示层安卓应用模块

data class BuildingModel(

    val id: String,

    val name: String,

    val location: LatLng,

    val tenants: List<TenantModel> = listOf()
)

BuildingEntity 取自外网 api.

这很好地将每个模块彼此分开,但在我的应用程序中,我有很多具有嵌套结构的不同实体.所以我最终写了很多 kotlin 数据类和映射器.

This nicely separates each modules from each other, but in my app I have a lot of different entities with nested structures. So I end up writing a lot of kotlin data classes and mappers.

我该如何简化?我可以删除 Building 类并在数据和域层上使用 BuildingEntity 吗?只是在表示层将 BuildingEntity 转换为 BuildingModel 吗?

How I can simplify this? Can I remove Building class and use BuildingEntity on data and domain layer? Just convert BuildingEntity to BuildingModel on presentation layer?

我正在尝试寻找实用的答案,人们是如何解决此类问题的,而不是编写大量数据类和映射器?

Im trying find practical answers, how people are solving this kind of problem, not ending up to writing tons of data classes and mappers?

推荐答案

在我的域模块中,我有我的模型作为接口(Kotlin 允许我们在接口内有 vals),数据模块中的实现和展示中​​根本没有模型.

In my domain module I have my models as interfaces (Kotlin allow us to have vals inside interfaces), the implemenations in data module and no models in presentation at all.

看看这个小样本:

域:

interface IUserModel {
    val id: String
    val name: String
}

interface UserRepository {
    fun getUserDetails(id: String): IUserModel
}

数据:

@Entity
data class UserEntity(
    @SerializedName("userId")
    override val id: String,
    override val name: String
) : IUserModel

class UserRepositoryImpl(val userDao: UserDao) : UserRepository {

    override fun getUserDetails(id: String): IUserModel {
        return userDao.getUser(id) //userDao.getUser returns a UserEntity
    }
}

介绍:

class UserDetailsViewModel(val userId: String, val userRepository: UserRepository) : ViewModel() {
    val userData: LiveData<IUserModel> = MutableLiveData()
    fun getUserData() {
        (userData as MutableLiveData).postValue(userRepository.getUserDetails(userId))
    }
}

没有映射器,没有大量的数据类.

No mappers, no tons of data classes.

我有几个采用这种结构的项目,有时需要一个映射器(将网络模型转换为数据库实体),但使用接口作为域中的模型可以大大减少冗长.

I have a couple of projects with this sctructure and sometimes a mapper is needed (convert network models to database entities) but the verbosity is widely reduced using interfaces as models in domain.

这篇关于干净的架构:在不同的层共享相同的模型/实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆