使用复杂的 DTO 创建域模型映射器

发布于 2025-01-10 02:17:32 字数 1919 浏览 0 评论 0原文

我使用 JSON to Kotlin 插件通过 Moshi 生成 DTO 类,以节省大量处理来自 API 的复杂 JSON 响应的时间。

只是为了让大家看看响应有多大

在此处输入图像描述

使用 Moshi 的示例 DTO

@JsonClass(generateAdapter = true)
data class AssetDto(
    @Json(name = "data")
    val `data`: List<AssetData> = listOf(),
    @Json(name = "status")
    val status: Status = Status()
)

@Parcelize
@JsonClass(generateAdapter = true)
data class Status(
    @Json(name = "elapsed")
    val elapsed: Int = 0,
    @Json(name = "timestamp")
    val timestamp: String = ""
) : Parcelable

@Parcelize
@JsonClass(generateAdapter = true)
data class AssetData(
    @Json(name = "id")
    val id: String = "",
    @Json(name = "metrics")
    val metrics: Metrics = Metrics(),
    @Json(name = "name")
    val name: String = "",
    @Json(name = "profile")
    val profile: Profile = Profile(),
    @Json(name = "symbol")
    val symbol: String? = ""
) : Parcelable

问题

  1. 我想知道从域模型中创建域模型的最佳方法是什么复杂的DTO类,无需手动对其进行编码。

  2. 我应该为 AssetDto 还是 AssetData 创建域模型?正如您所看到的,我有大量的值对象,但我不知道是否应该在每个值对象上创建一个域模型,或者可以重用 DTO 中的数据类。

现在,我使用 JSON 到 Kotlin 生成了另一堆纯数据类,这意味着我有几十个相同的数据类,看起来我仍然有义务手动设置每个值,这现在变成了一个完全的阻碍。我不确定是否应该继续实施映射器。

data class AssetDomain(
    var status: Status? = Status(),
    var `data`: List<Data>? = listOf()
)

data class Status(
    var elapsed: Int? = 0,
    var timestamp: String? = ""
)

data class Data(
    var id: String? = "",
    var metrics: Metrics? = Metrics(),
    var name: String? = "",
    var profile: Profile? = Profile(),
    var symbol: String? = ""
)

输入图片此处描述

I am using JSON to Kotlin plugin for generating DTO classes with Moshi to save a lot of time dealing with complex JSON response from APIs.

Just to give a glimpse how huge the response can be

enter image description here

Sample DTO using Moshi

@JsonClass(generateAdapter = true)
data class AssetDto(
    @Json(name = "data")
    val `data`: List<AssetData> = listOf(),
    @Json(name = "status")
    val status: Status = Status()
)

@Parcelize
@JsonClass(generateAdapter = true)
data class Status(
    @Json(name = "elapsed")
    val elapsed: Int = 0,
    @Json(name = "timestamp")
    val timestamp: String = ""
) : Parcelable

@Parcelize
@JsonClass(generateAdapter = true)
data class AssetData(
    @Json(name = "id")
    val id: String = "",
    @Json(name = "metrics")
    val metrics: Metrics = Metrics(),
    @Json(name = "name")
    val name: String = "",
    @Json(name = "profile")
    val profile: Profile = Profile(),
    @Json(name = "symbol")
    val symbol: String? = ""
) : Parcelable

Problems

  1. I want to know what is the best way to create Domain Model out of a complex DTO class without manually encoding it.

  2. Should I create Domain Model for AssetDto or AssetData? As you can see I have tons of value-object and I do not know if I should create a Domain Model on each of those value-object or it is okay to reuse the data class from DTO.

For now I generate another pile of plain data class using JSON to Kotlin which means I have dozens of identical data class and it looks like I am still oblige to manually set each values, this became a total blocker now. I am not sure if I should continue implementing mapper.

data class AssetDomain(
    var status: Status? = Status(),
    var `data`: List<Data>? = listOf()
)

data class Status(
    var elapsed: Int? = 0,
    var timestamp: String? = ""
)

data class Data(
    var id: String? = "",
    var metrics: Metrics? = Metrics(),
    var name: String? = "",
    var profile: Profile? = Profile(),
    var symbol: String? = ""
)

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謌踐踏愛綪 2025-01-17 02:17:32

您应该根据业务逻辑而不是响应本身创建域模型。

You should create domain models based on your business logic not based on the response itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文