jmfayard
08/02/2022, 4:21 PMUserProfileEntity
(sql) to UserProfile
who have the same properties
And I'm bored to write those mappers manually
In the .Net world, there is http://automapper.org/ for that
I wonder I could just leverage kotlinx.serialization
to transform UserProfileEntity
to JsonElement
and back to UserProfile
Javier
08/02/2022, 4:32 PMJavier
08/02/2022, 4:33 PMCasey Brooks
08/02/2022, 4:35 PMUserProfile
model that is essentially the same schema as the SQL table it comes from. You’re left with a similar problem of needing to update UserProfile
if UserProfileEntity
changes. So even though you’re not coupled to the DB model class itself, you’re still tightly coupled to the DB.
Plus, it doesn’t give you much room to improve the UserProfile
model for easier use (make it more type-safe, group related properties into nested objects, resolve IDs to concrete objects during mapping, etc). And any such transformations would probably be much harder to maintain with Serialization annotations or custom serializers than the simple, hand-written mapper functions. Not to mention that if you allow yourself to fallback to hand-written mappers, then you’re just fragmenting your code, making it harder for new developers on the project to know what they should be doing.jmfayard
08/02/2022, 4:36 PMjmfayard
08/02/2022, 4:36 PMCasey Brooks
08/02/2022, 4:42 PMJsonElement
can work very well as an intermediate stage in data-heavy use cases. They’re lightweight, immutable, easy to work with, multiplatform, and easy to debug. I’ve been experimenting with using it for generating large forms in Compose, https://github.com/copper-leaf/kotlin-json-forms.
Thinking of it in a similar way: convert a concrete domain model to JsonElement
, make changes to the data with this library, then convert the updated JSON back into the original (or another) model, all without needing to make a bunch of mapper functions by hand.jmfayard
08/02/2022, 4:44 PMJsonElement -> JsonElement
makes sense to add flexibility!Robert Jaros
08/02/2022, 4:53 PMmbonnin
08/02/2022, 6:38 PMto decouple sql entities from domain classes,
I have to write tons of dummy mappers from sayMy 2 cents: if you're doing this automatically, you're introducing the coupling back. If something changes in your SQL, you'll have to change your domain class(sql) toUserProfileEntity
who have the same propertiesUserProfile
joschi
08/03/2022, 6:50 AMjmfayard
08/03/2022, 6:52 AMkapt
and managed to keep it out of my project until now 🙂