When we write Java coding we might come across scenarios where we might need to convert data types | transform one type to another
eg: Let's take a simple REST call. We call GET department. First method call will be for Controller and Controller will return a department (DTO)object. To get this from Controller, we call service layer and from Service Layer Repository (DAO) will be called. From Dao we get and Entity object. In this scenario, we have two types of objects naming DTO and DAO.
This scenario comes where we normally does not expose Entity layer objects to API level.
So we need to transform DTO to DAO and vice versa.
To do this, we need to write a methods to Transform and reverse transform.
To do this easily we can use "Model Mapper"
http://modelmapper.org/getting-started/
We can simply call "map" method and no need to write Transform methods.
Below is the example.
public class Address {
private String street;
private String city;
public Address(String street, String city) {
this.street = street;
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
---------------------------------------------------------------------------------------
public class Customer {
private String name;
public Customer(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Gradle dependency
compileOnly('org.projectlombok:lombok')
compile group: 'org.modelmapper', name: 'modelmapper', version: '2.3.0'
eg: Let's take a simple REST call. We call GET department. First method call will be for Controller and Controller will return a department (DTO)object. To get this from Controller, we call service layer and from Service Layer Repository (DAO) will be called. From Dao we get and Entity object. In this scenario, we have two types of objects naming DTO and DAO.
This scenario comes where we normally does not expose Entity layer objects to API level.
So we need to transform DTO to DAO and vice versa.
To do this, we need to write a methods to Transform and reverse transform.
To do this easily we can use "Model Mapper"
http://modelmapper.org/getting-started/
We can simply call "map" method and no need to write Transform methods.
Below is the example.
public class Address {
private String street;
private String city;
public Address(String street, String city) {
this.street = street;
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
---------------------------------------------------------------------------------------
public class Customer {
private String name;
public Customer(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
----------------------------------------------------------------------------------------
public class Order {
private Customer customer;
private Address billingAddress;
private Address shippingAddress;
public Order(Customer customer, Address billingAddress, Address shippingAddress) {
this.customer = customer;
this.billingAddress = billingAddress;
this.shippingAddress = shippingAddress;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Address getShippingAddress() {
return shippingAddress;
}
public void setShippingAddress(Address shippingAddress) {
this.shippingAddress = shippingAddress;
}
public Address getBillingAddress() {
return billingAddress;
}
public void setBillingAddress(Address billingAddress) {
this.billingAddress = billingAddress;
}
}
---------------------------------------------------------------------------------------------
import org.modelmapper.ModelMapper;
public class FlatteningExample1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Customer customer = new Customer("Joe Smith");
Address billingAddress = new Address("2233 Pike Street", "Seattle");
Address shippingAddress = new Address("1234 Market Street", "San Francisco");
Order order = new Order(customer, billingAddress, shippingAddress);
System.out.println(customer);
ModelMapper modelMapper = new ModelMapper();
OrderDTO dto = modelMapper.map(order, OrderDTO.class);
System.out.println(dto);
System.out.println(dto.getCustomerName() + "= " + order.getCustomer().getName());
}
}
You can further remove getter, Setters and other boiler plate code using "lombok"
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class LAddress {
private String street;
private String city;
}
--------------------------------------------------------------------------------------------------------------
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class LCustomer {
private String name;
}
--------------------------------------------------------------------------------------------------------------
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class LOrder {
private LCustomer customer;
private LAddress billingAddress;
private LAddress shippingAddress;
}
--------------------------------------------------------------------------------------------------------------
import org.modelmapper.ModelMapper;
public class LFlatteningExample1 {
public static void main(String[] args) {
String name = "Joe Smith";
String street = "2233 Pike Street";
String city = "Seattle";
String shipStreet = "1234 Market Street";
String shipCity = "San Francisco";
LCustomer customer = LCustomer.builder().name(name).build();
LAddress billingAddress = LAddress.builder().street(street).city(city).build();
LAddress shippingAddress = LAddress.builder().street(shipStreet).city(shipCity).build();
LOrder order = LOrder.builder().customer(customer).billingAddress(billingAddress)
.shippingAddress(shippingAddress).build();
System.out.println(customer);
ModelMapper mapper = new ModelMapper();
LOrderDTO orderDTO = mapper.map(order, LOrderDTO.class);
System.out.println(orderDTO);
}
}
--------------------------------------------------------------------------------------------------------------
Gradle dependency
compileOnly('org.projectlombok:lombok')
compile group: 'org.modelmapper', name: 'modelmapper', version: '2.3.0'
No comments:
Post a Comment