Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
pavan bukka
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic