Hey, is there anything like partial classes in Kot...
# getting-started
o
Hey, is there anything like partial classes in Kotlin? Lets say that I have a Post model with (id, title, content, createdAt, updatedAt) and I want to have only the primary fields (title, content) for the DTO Which way would be the most efficient to handle that in Spring Boot with Kotin
r
No, unfortunately not. I recall seeing a third-party dependencies (perhaps a compiler plugin) that accomplished that, but idk how reliable that would be.
The most pragmatic solution is just to make separate DTOs per use case. So
CreatePostDto
or
CreatePostRequest
(pick your poison) only has title and content, and
ReadPostDto
has all attributes, etc.
o
So I would have to create both the
Entity
and the
Dto
seperately
A little bit repeating but probably won't harm anything 😄
r
A little bit repeating but probably won't harm anything
While it may seem a bit awkward, I'd argue that it's a good approach. That way your DTO models aren't tied to your entity models.
Of course if this is just a tiny CRUD app or whatever that probably doesn't matter. But it can be useful in large apps.
p
Also you may wish to have extension methods to convert between them, e.g.
Copy code
fun Post.toDTO() = PostDTO(title, content)
o
Great, thanks guys
I just want to ask a last question
Can I use
Copy code
modelmapper:modelmapper
on Kotlin?
So that I can convert my DTO to Entity