Reputation: 3469
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
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? = ""
)
Upvotes: 3
Views: 1447
Reputation: 26
You should create domain models based on your business logic not based on the response itself.
Upvotes: 0