Hi! I’ve been developing some small web app in Spring lately. I just started wondering, how do you people handle mapping objects, especially requests into entities. E.g.:
data class TitleCreateRequest( val title: String, )
into:
@Document("titles") data class Title( val title: String, var id: String? )
Most of the time I use extension functions like:
fun TitleCreateRequest.toDomain() = Title(title = this.title)
and I think this is pretty expressive in code, especially that I use this in whole project. But I started wondering: I somehow make TitleCreateRequest responsible for creation of another (more important?) object. Change in Title`may require change in that extension function which is probably somewhere else than in Title.kt`file. On the other hand I actually expose Title primary constructor like that.
What do you think about defining secondary constructor in Title?
@Document("titles") data class Title( val title: String, var id: String? ) { constructor(request: TitleCreateRequest) : this(title = request.title) }
The other way around mapping – from entity to dto – I think extension function is the best, because it is entity’s responsibility to define how it should be presented publicly.
So how do you handle this?
submitted by /u/DiethylMetaToluamide
[link] [comments]