Basics Of ModelMapper
- Digital Engineering
Basics Of ModelMapper
Today, we are going to look at a basic overview of ModelMapper in a spring boot application.
Basically, Modelmapper is a object mapping library which is used for mapping to map DTOs into entities and vice-versa.
We are going to utilise a package called ModelMapper to map DTOs into entities and vice versa without having to create laborious/boilerplate code. By automatically calculating how one object model translates to another, ModelMapper aims to make object mapping simple.
This library supports an alot of options to speed up the mapping process and is fairly powerful, but it also emphasises convention over configuration by offering a default approach that works in most situations.
How It works?
ModelMapper consists of two distinct processes: the matching process, which matches the properties of a source and destination type, and the mapping process, which converts matched property values from a source to a destination object. Here is a more detailed explanation of these procedures.
-
Matching Process: Which source and destination properties match each other is determined by the matching process, which makes use of conventions set up in a ModelMapper or TypeMap. To discover source to destination matches, the procedure first identifies appropriate attributes, modifies and tokenizes their names, and then uses those tokens.
-
Maping Process: The mapping will take place in accordance with the Mappings specified in the TypeMap if there is a TypeMap for the source and destination types. If a Converter is available, and it can convert the source object to the desired type, mapping will take place via the Converter otherwise.
Getting Started
For starting with modelmapper in spring boot, you need to add the below dependency in pom.xml file:
1 2 3 4 5 |
<dependency> <groupId>org.modelmapper</groupId> <artifactId>modelmapper</artifactId> <version>3.0.0</version> </dependency> |
Now Let’s start with Mapping. Suppose you are having following source model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Assume getters and setters on each class class PassBook{ Bank bank; Customer customer; } class Bank{ String name; Address address; } class Customer{ String firstName; String lastName; } class Address{ String street; String city; } |
Now, you are having a following requestDto
1 2 3 4 5 6 7 |
class PassBookDTO{ String bankName; String bankStreet; String bankCity; String customerFirstName; String customerLastName; } |
We can use ModelMapper to implicitly map an passBook instance to a new PassBookDTO:
1 2 |
ModelMapper modelMapper = new ModelMapper(); PassBookDto passBookDTO = modelMapper.map(passBook , PassBookDto.class) |
1 2 |
assertEquals(passBook.getCustomer().getFirstName(), passBookDTO.getCustomerFirstName()); // Similarly for other properties. |
Thus! We’ve learned how to map objects with safety and ease.
Some Learning Resources
Thanks.
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s