Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring DTO-DAO (Resource - entity) mapping goes in which application layer: Controller or Service?

I'm writing a Spring (4.1.7) web application that exposes a RESTful service and wish to use DTO "resource" objects for communication between Controller and client browser rather than expose my persistence entities.

Currently the application has the following layers:

  • View (JSP/JSON)
  • Controller(s)
  • DAO (@Service)
  • DAO (@Repository)

My question is, where should I be mapping my DAO entities to DTO resources? I had a look at some examples using Spring HATEOAS and they show Resource objects extending ResourceSupport being mapped in the Controller. Is this the best way to do it, or should I be returning resources from the DAO Service?

I wish to add Link elements to the returned resource (for self and related resources), but can't see how Link elements would be resolved if processed in the Service without it having knowledge of the Controller and it's @RequestMapping. On the other hand, I don't know if it's good practice to clutter the Controller with the mapping either.

like image 342
Tiksi Avatar asked Jul 27 '15 02:07

Tiksi


People also ask

Can we use DTO in service layer?

DTOs (Data Transfer objects) are the objects or classes used to transfer data between layers through the service layer. The service layer only accepts data in the form of DTOs.

What is service layer and DAO layer?

Generally the DAO is as light as possible and exists solely to provide a connection to the DB, sometimes abstracted so different DB backends can be used. The service layer is there to provide logic to operate on the data sent to and from the DAO and the client.

How do you map entity to DTO?

Thus, we can use model mapper to convert entity to dto or dto to entities. First, we need to add model mapper dependency. Next, we can create a @Bean factory method to create ModelMapper instance. This way, the model mapper instance will be available for injection on the application level.

What is DAO and DTO in spring?

It is an Data Transfer object which used to pass the properties from service layer to persistence layer. DAO: It is an Data Access object. it is also known as persistence layer.

What is the use of DTO layer in spring boot?

In Spring Framework, Data Transfer Object (DTO) is an object that carries data between processes. When you're working with a remote interface, each call is expensive. As a result, you need to reduce the number of calls. The solution is to create a Data Transfer Object that can hold all the data for the call.


1 Answers

DTO ( Data Transfer Object) as obvious in its name, is used for transfering data out of your application. In your case the best place to put them is in your controller layer. You should only expose DTO's to UI and when you get a data from UI you should convert it to the business entity and call the below tier. The reason is, in this way you are free to change the business entitiy without breaking UI and leads to better maintenance. Also your business/DAO tier should be unaware of UI and DTO's for the same reason. So the best place to convert DTO to Business Entities and vice versa in your app is Controller tier.

PS:Take a look at Dozer too ;)

like image 147
Morteza Adi Avatar answered Sep 28 '22 07:09

Morteza Adi