• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Mike London

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

Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Posts: 17347
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

    Bookmark Topic Watch Topic
  • New Topic