Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is DTO pattern deprecated or not?

In a complete Java EE application that's clustered is the DTO pattern still a valid option? The application in question uses EJBs Hibernate and Struts with Spring etc. Is there anything wrong with transferring domain objects in such a scenario?

EDIT: Just to clarify my question, with modern day resources and improvements in Java EE is there a reason not to just use domain objects? If there is not then isn't DTO pattern sort of fading out and shouldn't be used in new applications?

like image 913
Thihara Avatar asked Jun 28 '12 04:06

Thihara


2 Answers

Is not deprecated. It depends on the application architecture if the DTO pattern should be used or not. For example, when you develop Web Services (using JAX-WS or JAX-RS), you should send DTO's over your web methods so a C# or Python client application may consume it, and your web method should not return an object which class has Hibernate annotations, remember than in other languages the Entity won´t be created with those annotations or other business logic inside.


EDIT (Based in your comment): That depends on the software architecture. For example, I'm working on a SOA project and we use DTO's for the Services Layer and the Presentation Layer. More deeper inside, we even use DTO's to handle database communication inside the services, we use only SP's to communicate with DB, so no Hibernate or any other ORM tools can work there, we could use Spring DAO and that framework uses DTO's too. You can find lots of DTO pattern in many applications nowadays.

More info that would be great for this question:

  • Difference between DTO, VO, POJO, JavaBeans? (where basically, any DTO is a POJO).
  • Core J2EE Patterns - Transfer Object

EDIT 2: Another source of information that will explain the main reason for using DTO's design, explained by Martin Fowler

  • LocalDTO

Conclusion: DTO's are not an anti pattern. DTO's are meant to be used only when you need to pass data from one subsystem to another and they don't have a default or standar way to communicate.

like image 187
Luiggi Mendoza Avatar answered Oct 05 '22 19:10

Luiggi Mendoza


It is a very useful pattern in Java EE.

I use a DTO to transfer related entity objects from EJB beans to the UI layer. The entity objects are fetched from DB in one transaction (see TransactionAttributeType.REQUIRED) and stored in the DTO object. The DTO is consumed in the UI layer.

like image 26
Martin Mecera Avatar answered Oct 05 '22 18:10

Martin Mecera