Normally, when your are designing an enterprise application, your domain model is contained in your application's service layer. Over this layer (controllers), the communication is performed using DTOs, then you only are transfering the required information by your different clients (web user interface, another backend application, mobile applications and so on). It is in this point where Orika can help us.
Let me show you an example. Imagine you have this domain object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hdbandit.orika_mapper; | |
public class Person { | |
private String name; | |
private String surname; | |
private int age; | |
private String address; | |
private String jobLocation; | |
private int salary; | |
private String jobCategory; | |
public String getJobLocation() { | |
return jobLocation; | |
} | |
public void setJobLocation(String jobLocation) { | |
this.jobLocation = jobLocation; | |
} | |
public int getSalary() { | |
return salary; | |
} | |
public void setSalary(int salary) { | |
this.salary = salary; | |
} | |
public String getJobCategory() { | |
return jobCategory; | |
} | |
public void setJobCategory(String jobCategory) { | |
this.jobCategory = jobCategory; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getSurname() { | |
return surname; | |
} | |
public void setSurname(String surname) { | |
this.surname = surname; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public void setAddress(String address) { | |
this.address = address; | |
} | |
@Override | |
public String toString() { | |
return "Person{" + | |
"name='" + name + '\'' + | |
", surname='" + surname + '\'' + | |
", age=" + age + | |
", address='" + address + '\'' + | |
", jobLocation='" + jobLocation + '\'' + | |
", salary=" + salary + | |
", jobCategory='" + jobCategory + '\'' + | |
'}'; | |
} | |
} |
Imagine you need two different endpoints to serves information about persons. One of them must to return a basic person information, and the other one must to return the complete information about the person. As you can imagine, you need two DTOs to transfer the required information into the body's response (using json or whatever you want).
Here are our DTOs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PersonBasicInfoDTO { | |
private String name; | |
private String surname; | |
private int age; | |
private String address; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getSurname() { | |
return surname; | |
} | |
public void setSurname(String surname) { | |
this.surname = surname; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public void setAddress(String address) { | |
this.address = address; | |
} | |
@Override | |
public String toString() { | |
return "PersonBasicInfoDTO{" + | |
"name='" + name + '\'' + | |
", surname='" + surname + '\'' + | |
", age=" + age + | |
", address='" + address + '\'' + | |
'}'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PersonCompleteInfoDTO { | |
private String name; | |
private String surname; | |
private int age; | |
private String address; | |
private String jobLocation; | |
private int salary; | |
private String jobCategory; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getSurname() { | |
return surname; | |
} | |
public void setSurname(String surname) { | |
this.surname = surname; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public void setAddress(String address) { | |
this.address = address; | |
} | |
public String getJobLocation() { | |
return jobLocation; | |
} | |
public void setJobLocation(String jobLocation) { | |
this.jobLocation = jobLocation; | |
} | |
public int getSalary() { | |
return salary; | |
} | |
public void setSalary(int salary) { | |
this.salary = salary; | |
} | |
public String getJobCategory() { | |
return jobCategory; | |
} | |
public void setJobCategory(String jobCategory) { | |
this.jobCategory = jobCategory; | |
} | |
@Override | |
public String toString() { | |
return "PersonCompleteInfoDTO{" + | |
"name='" + name + '\'' + | |
", surname='" + surname + '\'' + | |
", age=" + age + | |
", address='" + address + '\'' + | |
", jobLocation='" + jobLocation + '\'' + | |
", salary=" + salary + | |
", jobCategory='" + jobCategory + '\'' + | |
'}'; | |
} | |
} |
Take a look.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); | |
// map Person-PersonBasicInfoDTO | |
mapperFactory.classMap(Person.class, PersonBasicInfoDTO.class) | |
.byDefault() | |
.register(); | |
// map Person-PersonCompleteInfoDTO | |
mapperFactory.classMap(Person.class, PersonCompleteInfoDTO.class) | |
.byDefault() | |
.register(); | |
MapperFacade mapper = mapperFactory.getMapperFacade(); | |
Person person = new Person(); | |
// set some field values | |
person.setAddress("New York"); | |
person.setAge(32); | |
person.setName("Peter"); | |
person.setSurname("Parker"); | |
person.setJobLocation("New York"); | |
person.setJobCategory("Super Hero"); | |
person.setSalary(0); | |
// get basic info | |
PersonBasicInfoDTO basicInfoDTO = mapper.map(person, PersonBasicInfoDTO.class); | |
// get complete info | |
PersonCompleteInfoDTO completeInfoDTO = mapper.map(person, PersonCompleteInfoDTO.class); |
In fact, this example is very simple, but Orika supports more complex mappings. You can find a complete Orika's guide here.
Happy new year!
No comments:
Post a Comment