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

althaf

08/14/2021, 3:09 PM
Hi i have a data class in domain layer and data layer, both has same attribute names, is there way in kotlin to initialized the domain model with the data model, if they have same attribute names ? domain layer model data class JourenyId ( val jr001, val jr002 ... ) data layer data class ApIResponseJourenyId ( val jr001, val jr002 ... )
e

ephemient

08/14/2021, 7:00 PM
possible with reflection. but I do not recommend. you want something that will check that the types all align - and for these types of mappings between layers I would expect that you will eventually need to transform some of the properties as well - while this will provide you no compile-time warnings and instead fail at runtime
👍 1
t

Tomasz Krakowiak

08/16/2021, 9:45 AM
I guess redundancy is in place to provide stable API separated from business logic. I think the best solution for this kind of problem is implementing plugin for build system or IDE, that would generate mappings automatically. This is requires a little additional work and won't pay off in small project. So if it's small project, I recommend implementing mapping manually. Reflection should be avoided due to various reasons. I understand you're a beginner since you posted on getting-started, so I recommend manual approach. One you feel comfortable enough I recommend looking into compiler plugins and code generation. May the force be with you!
2 Views