10

Currently we are using ModelMapper in our project. However in the site i see there are lot of likes for MapStruct.

Not sure the differences and whether we need to really go for an upgrade.

What are the differences between ModelMapper and MapStruct ?

Thanks.

Filip
  • 19,269
  • 7
  • 51
  • 60
Java Architect
  • 201
  • 3
  • 5

1 Answers1

20

(Project lead of MapStruct here, so naturally I am biased)

I have not used ModelMapper before. However, the projects are quite different in the way they are doing the mapping. I believe that ModelMapper is based on reflection and performs the mapping during runtime. Whereas MapStruct is a code generator which generates the mapping code (java classes) during compilation time.

So naturally if you are worried about performance then MapStruct is the clear choice. There is this independent Java Object Mapper Benchmark that benchmarks different frameworks.

The code that is generated by MapStruct is human readable code which is easy to debug and there is no reflection in it.

There are a lot of built-in conversions.

You get notified during compilation time if something could not be mapped and you would be responsible to providing such mappings so MapStruct can use them.

There are also IDE plugins: * IntelliJ plug-in: helps when editing mapper interfaces via auto-completion, go to referenced properties, refactoring support etc. * Eclipse plug-in avaible: has quickfixes and auto-completions which are very helpful when designing mapper interfaces

Filip
  • 19,269
  • 7
  • 51
  • 60
  • Do you support mapping in an easy way from snake case fields to camel case ? For example, if I have a field called "number_of_carts" and I would like to map it to "numberOfCarts" - do you have support for that similar to the NameTokenizer that ModelMapper has? – Yair Zaslavsky Feb 13 '22 at 21:46
  • 1
    Yes there is a way, but I can't really answer it in the comments. I would suggest to you to create a new question – Filip Feb 14 '22 at 18:20
  • I want to fix circular references combined with lists that are very common and I can't find how to do it. Even extra custom code shouldn't be required for this, it's such a common case for enterprises. We deal with 250 models that need mapping, and it's "under discussion" after years after first release? Nobody talks about it but dude. The time I've spent looking on a solution, I've had mapped all the entities manually tbh. May be good for hobbyists though, not enterprises with complex domain models. Not enough analysis done on the use cases. – html_programmer Apr 24 '23 at 21:16