Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good to upgrade to MapStruct from ModelMapper?

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.

like image 801
Java Architect Avatar asked Feb 06 '20 13:02

Java Architect


People also ask

Which is better ModelMapper or MapStruct?

MapStruct rules. There. MapStruct is nice for simple mappings.

Is it good to use MapStruct?

Excellent performance, as no reflection or byte code generation at runtime is needed; the generated code contains plain method invocations, just as if the mapper was hand-written. No runtime dependencies, making MapStruct a great solution for Android applications.

Is MapStruct slow?

Throughput. In throughput mode, MapStruct was the fastest of the tested frameworks, with JMapper a close second.

Why do we need MapStruct?

MapStruct is an open-source Java-based code generator which creates code for mapping implementations. It uses annotation-processing to generate mapper class implementations during compilation and greatly reduces the amount of boilerplate code which would regularly be written by hand.


1 Answers

(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

like image 102
Filip Avatar answered Nov 11 '22 13:11

Filip