使用复杂的 DTO 创建域模型映射器
我使用 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
问题
我想知道从域模型中创建域模型的最佳方法是什么复杂的DTO类,无需手动对其进行编码。
我应该为
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
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
I want to know what is the best way to create Domain Model out of a complex DTO class without manually encoding it.
Should I create Domain Model for
AssetDto
orAssetData
? 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? = ""
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该根据业务逻辑而不是响应本身创建域模型。
You should create domain models based on your business logic not based on the response itself.