martedì 12 aprile 2016

What makes JMapper unique

Regardless of the many features already mentioned on several occasions, the feature that makes JMapper unique compared to the competition is the separation between the  relation between variables and mapping logic.

What is meant by relation between variables?
It means the mapping configuration with one of the three permitted types, for example:

class User {              class UserDTO {

   @JMap String name;               String name;
   @JMap Integer age;               Integer age;
   @JMap String address;            String address;

   // getters and setters..         // getters and setters..

}                                }

What is meant by mapping logic?
it means that you can choose what to take from the source and which fields of destination must be mapped.

There may be cases in which the relationship is the same but must be taken into account only valued source fields, or in the case of enrichment, to be considered only the null destination fields.

Issue
we need to combine the data from the database with the user entries, many fields are optional and the user only has updated some of their own profile fields.

First of all we need of a Jmapper instance:

JMapper<UserDTO, User> mapper = new JMapper<>(UserDTO.class, User.class);

1° solution

We take only the valorized fields defined from user keeping the previous values.

mapper.getDestination(userDTO, user, MappingType.ALL_FIELDS, MappingType.ONLY_VALUED_FIELDS);

What we are saying with this code is to consider only the fields of source that are valorized.

2° solution

We also want to keep the fields left empty by user.

mapper.getDestination(userDTO, user, MappingType.ALL_FIELDS, MappingType.ALL_FIELDS);

In this case all field are mapped, also null fields.

The results of this two solutions are different but the configuration is the same!
This is the key of JMapper, the separation between configuration and mapping logic.

With any other library you have to create a configuration for each type of mapping that you want to implement!

This is definitely a topic on which reason before choosing which framework to use.