https://kotlinlang.org logo
#getting-started
Title
# getting-started
o

Okan Yıldırım

05/06/2022, 8:36 PM
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

Richard Gomez

05/06/2022, 8:39 PM
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

Okan Yıldırım

05/06/2022, 8:41 PM
So I would have to create both the
Entity
and the
Dto
seperately
A little bit repeating but probably won't harm anything 😄
r

Richard Gomez

05/06/2022, 8:45 PM
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

phldavies

05/06/2022, 8:46 PM
Also you may wish to have extension methods to convert between them, e.g.
Copy code
fun Post.toDTO() = PostDTO(title, content)
o

Okan Yıldırım

05/06/2022, 8:49 PM
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