Converting from one object structure to another is often time consuming, without adding real value.
I recently came across the Modelmapper library, which reduces this task to a minimum.
Add the dependency below to either your gradle og pom file.
External Basket object
Assuming the basket object should be mapped to a BasketDTO, performing the mapping is simple:
I recently came across the Modelmapper library, which reduces this task to a minimum.
Add the dependency below to either your gradle og pom file.
Gradle
compile "org.modelmapper:modelmapper:2.3.0"
Maven
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.0</version>
</dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.0</version>
</dependency>
ModelMapper configuration
There is a wide range of configuration options, which can be found hereModelMapper
Internal basket object
public class Basket {
private UUID id;
private String productCode;
private String customerName;
private String orderContactEmail;
private String orderContactPhoneNo;
// Assume getters and setters
}
private UUID id;
private String productCode;
private String customerName;
private String orderContactEmail;
private String orderContactPhoneNo;
// Assume getters and setters
}
External Basket object
public class BasketDTO {
private String id;
private String productNo;
private String customerName;
private String orderContactEmail;
private String orderContactPhoneNo;
// Assume getters and setters
}
private String id;
private String productNo;
private String customerName;
private String orderContactEmail;
private String orderContactPhoneNo;
// Assume getters and setters
}
Assuming the basket object should be mapped to a BasketDTO, performing the mapping is simple:
final ModelMapper modelMapper = new ModelMapper();
final BasketDTO dto = modelMapper.map(basket, BasketDTO.class);
final BasketDTO dto = modelMapper.map(basket, BasketDTO.class);
Comments
Post a Comment