Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModelMapper - Converter/ AbstractConverter vs Provider

I'm using ModelMapper to convert some objects to complex DTOs and vice-versa.

Even though I've tried to understand the documentation, I've found hard to understand when to use a Converter or a Provider or an AbstractConverter.

Now, for example, if I want to convert String properties to little DTOs inside of the destination DTO, I'm doing it manually inside an abstract converter.

For instance:

dest.setAddressDTO(new AddressDTO(source.getStreet(), source.getNumber()));

Is though that the right way to do it? When should I use a provider?

And if I want to set properties with conditionals, can I use Conditional from within the converter or that's only when using a PropertyMap ?

Additionally, is it a good practice to use the same modelMapper instance to convert several different types of objects?

Thanks in advance

like image 950
magnoz Avatar asked Feb 05 '18 17:02

magnoz


People also ask

Does ModelMapper use reflection?

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.

How does ModelMapper work?

ModelMapper consists of two separate processes: the matching process, where a source and destination type's properties are matched to each other, and the mapping process where matched property values are converted from a source to destination object. A further description of these processes follows.

How do you use a model mapper converter?

The first is by adding the converter to a ModelMapper: modelMapper. addConverter(personConverter); This, in turn, sets the converter against the TypeMap corresponding to the source and destination types Person and PersonDTO .

What is ModelMapper in Spring boot?

ModelMapper, is an object-to-object framework that converts Java Beans (Pojos) from one representation to another. It automates different object mappings with a "convention follows … ...more.


1 Answers

The right way to work with this is to use Converters.

For example, let's say I want to create a converter to convert a dto into a domain object.

So first you define a converter:

private Converter companyDtoToCompany = new AbstractConverter<CompanyDto, Company>() {
    @Override
    protected Company convert(CompanyDto source) {
        Company dest = new Company();

        dest.setName(source.getName());
        dest.setAddress(source.getAddress());
        dest.setContactName(source.getContactName());
        dest.setContactEmail(source.getContactEmail());
     (...)
        dest.setStatus(source.getStatus());

        return dest;
    }
};

Then you add it to the mapper in the configureMappings() method:

    modelMapper = new ModelMapper();

    // Using STRICT mode to prevent strange entities mappin

    modelMapper.getConfiguration()
                     .setMatchingStrategy(MatchingStrategies.STRICT);

    modelMapper.addConverter(companyDtoToCompany);
    // modelMapper.addConverter(otherConverter);
}

And finally you just need to add the mapping methods for those types you can use from your code:

public Company convertCompanyReqDtoToCompany(CompanyDto dto, Class<Company> destinationType) {
        return modelMapper.map(dto, destinationType);
    }
like image 70
magnoz Avatar answered Oct 26 '22 23:10

magnoz