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.

ajay
  • 9,402
  • 8
  • 44
  • 71
  • 1
    Possible duplicate of [Java mapping: Selma vs MapStruct](https://stackoverflow.com/questions/34786737/java-mapping-selma-vs-mapstruct) – tkruse Jan 17 '18 at 02:15
  • 1
    Related list of mappers: https://stackoverflow.com/questions/1432764/ – tkruse Jan 17 '18 at 02:15

3 Answers3

15

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;
    }
alltej
  • 6,787
  • 10
  • 46
  • 87
  • 12
    Did you ever run a benchmark to compare it? Could you elaborate why you changed? – real_paul Feb 27 '18 at 07:49
  • 1
    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 Apr 22 '23 at 11:35
  • @Nesd Agree. My response was to address the question with a given example. – alltej Apr 27 '23 at 15:42
8

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.

ajay
  • 9,402
  • 8
  • 44
  • 71
Allanh
  • 465
  • 1
  • 7
  • 19
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

alltej
  • 6,787
  • 10
  • 46
  • 87
  • I was facing the same issue and the benchmark link helps a lot in making decision. – Ace Zachary Jan 17 '18 at 04:30
  • 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`) – alltej Jan 30 '18 at 19:50
  • 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`). – payne Sep 16 '21 at 14:11