Is there something like ModelMapper for Kotlin

Hi, I’m in a situation where I’m mapping from DTOs to command classes etc, and frequently a lot of the fields are the same between the source and target classes. Given Kotlin’s predisposition to final, etc it’s not that easy to use tools like ModelMapper. Are there more “Kotlinish” ways to address this scenario?

2 Likes

It’s three years since the question was asked … is there a recommendable mapping library for Kotlin now? Or is one of the existing Java libs like ObjectMapper or MapStruct particularly suitable for Kotlin?

1 Like

The question is incomplete. Are you taking about ORMs? If so have a look at Exposed or Ktorm, otherwise you could go for the ol’ fashioned Hibernate.

If you are talking about deserialization, GSON, Jackson and Kotlinx are the answers.

By the way, those solutions could have been easily found by googling something like “kotlin orm” or “kotlin deserialization”. Google is your friend, use it.

Furthermore, everything that works in Java, works in Kotlin/JVM. There is little reason to not to use a Java native library just because it is written in Java.

Tools like ModelMapper and MapStruct are pretty different from ORMs or serializers like Jackson. A tool like ModelMapper is for mapping one Java object to another Java object (and not to a database or Json or XML).

I’ve seen an older complaint about ModelMapper with Kotlin and was interested if there is an object-to-object mapper particularly suitable for Kotlin. Maybe there are problems with everything final or something like that.

2 Likes

I may not be familiar with the “object-to-object mapping” problem, but I do not see any particular issue in creating a function that copies the values of one object of, say, class A to an object of class B.

I think what the OP is asking for is a library that does the mapping automatically using some sort of configuration and guessing to figure out how to map one object to another. From what I can tell by googling the libs medium mentioned they use either reflection or annotation processing to do that.

I don’t personally like the reflection approach, because I think it’s to likely to create errors (especially when updating dependencies), but I can see why the code generation variant is useful. Sadly I don’t know of any projects for kotlin like this. Not sure how well the mentioned java libraries work with kotlin though since I never used any of them.

Then you might want to check out the frameworks before giving advice. :wink: Examples for Java object mapping frameworks are: Dozer, Orika, MapStruct, ModelMapper, etc.

There are some known problems like the need to use kotlin no-args plugin for having default no-arg constructor with data classes. Therefore, the question from the OP is reasonable.

I made it.
(But, it is beta. I’m improving it.)

There was a mistake that prevented it from being successfully deployed from JitPack.
Currently fixed(version 0.7).

Also, the problematic version (0.4 to 0.6) has been deleted.