11

Hi, I am trying to use mapping framework in my project, but i am not able to decide which one to choose, among these three mapping frameworks. Selma v/s MapStruct v/s Model Mapper mapping framework ?

Please help me to choose the best feature and performance oriented framework.

2

3 Answers 3

17

In light of the Java 8 Stream APIs and with lombok annotation processor library, I am no longer using this mapping frameworks. I create my own mappers by implementing the Converter interface of the spring framework (package org.springframework.core.convert.converter.Converter)

 @Component public class ProductToProductDTO implements Converter<Product, ProductDTO> { @Override public ProductDTO convert( Product source ) { ProductDTO to = ProductDTO.builder() .name( source.getName()) .description( source.getDescription() ) .tags(source.getTags().stream().map(t -> t.getTag().getLabel()).collect( Collectors.toList())) .build(); return to; } } @Builder //lombok annotation to create Builder class @Data //lombok annotation to create getters & setters @AllArgsConstructor //required for @Builder public class ProductDTO { private String name; private String description; private List<String> tags; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Did you ever run a benchmark to compare it? Could you elaborate why you changed?
alltej: If you use this method for a DTO pattern, it adds quite a lot of complexity to your program because you have to create at least one converter every time you create a new DTO. If you also want to convert backwards, you will need 2 converters. In addition, you have to set each field manually, every time you create a new Dto. So for 10 dtos, you need to create a total of 30 files in the worst case. On top of that, let's say there are 10 fields for each dto, then you have to write 30*10 = 300 lines of code. It doesn't sound good.
@Nesd Agree. My response was to address the question with a given example.
9

You can find a complete comparison between the most used frameworks to map when you click here. In this link you can find benchmark comparisons, how to use each framework etc. I decided to use MapStruct because it is easy to use and is so fast.

1 Comment

I agree. According to Baeldung, JMapper is slightly more performant, but MapStruct is really easier.
7

I was in similar situation before while looking for solution to leverage annotation processors. I would go with either Selma or MapStruct. Kinda similar being code generators. Played with both but went with MapStruct. Similar question here in SO can help you - Java mapping: Selma vs MapStruct

Also a project in github that benchmarked object to object mapper frameworks has Selma and MapStruct in the top - https://github.com/arey/java-object-mapper-benchmark

3 Comments

I was facing the same issue and the benchmark link helps a lot in making decision.
In light of the Java 8 Stream APIs, I am no longer using this mapping frameworks. I create my own mappers by implementing the Converter interface of the spring framework (package org.springframework.core.convert.converter.Converter)
As a simple justification for some of those results: it is clear that compile-time mapping class generation (like MapStruct) is much faster than runtime mappers that use reflection (like ModelMapper).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.