Friday, January 1, 2016

Mapping beans with Orika library

Today I want to talk about one of this useful tools that sometime can do our life more easy :).

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.


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.



Well, at this point you have to transform the response of our service (service layer) that returns a domain object into the different DTOs required by the different endpoints. Orika can do that in a simple way. The key concepts are MappingFactory and MappingFacade. Orika by default maps the properties with the same name and type.

Take a look.


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