GeeCON Prague 2014*
The moose likes Spring and the fly likes whether to use dto or model for transfering the data from one layer to another layer. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Spring
Bookmark "whether to use dto or model for transfering the data from one layer to another layer." Watch "whether to use dto or model for transfering the data from one layer to another layer." New topic
Author

whether to use dto or model for transfering the data from one layer to another layer.

pavan bukka
Greenhorn

Joined: Jan 01, 2013
Posts: 21
Hi all,
we have developed an application using spring and hibernate, here we are actually passing model objects from dao layer to service and from service to controller and from there to jsp . we are suggested by someone that models shouldn't be sent from one layer to other instead use DTO's to transfer the data from one layer to another layer. we are confused that should we go for dto's or we go with models only. please tell us which is the correct way to transfer the data and why.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Well the real answer is "it depends"

First, I will say this and mean it very much DTO is an ANTI-PATTERN. Meaning it was created to fix a major poor design in Entity Beans in Java EE.

Now with that in mind. There are some domain objects or use cases where you don't want DTOs and you want your POJO domain model. That to me is at least 80% of the time you will send those objects from one layer to the other.

If you create DTOs for all those layers then you will have a lot of "Adapter" code to convert your domain objects to DTOs and back the other way. This is extremely unmaintainable and error prone code. Personally I would run far far away if I ever saw this code in a project I was to work on. I have seen it way too many times and it is sad.

Now there are some use cases where you need "reports" or report data" This tends to be a report, where you need data from lots of tables combined to make a page, and creating a large graph of related domain objects can be well complex. Then a Report Domain object (almost like a DTO) would be better than data domain objects.

But there are ORM tools that make those simpler, like iBatis and even JPA/Hibernate has things like "Select new ReportDomain(field1, field2, field3) from Domain d" etc

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
GeeCON Prague 2014
 
subject: whether to use dto or model for transfering the data from one layer to another layer.