DTO or not to DTO?
Omar Ismail
Senior Software Engineer @ Digitinary | Java 8 Certified? | Spring & Spring Boot?????? | AWS? | Microservices ?? | RESTFul Apis & Integrations ?? FinTech ?? | Open Banking ?? | Digital Payments and Transformation??
Thank to the original creator : https://medium.com/javarevisited/dto-or-not-to-dto-58259d4228ec
DTO or not to DTO?
Disclaimer:?This article does not take the existence of the?Java records ?into consideration, and it merely speaks about DTOs, in their classical understanding.
Shall I use DTOs or not?
This is a long-time popular, to some extent debatable, and sometimes, even inclining to so-called “holly war” question, very short answer to which, is
— It depends.
— What is DTO, anyways?
— It is a?Data Transfer Object,?i.e. the data aggregate object, sole purpose of which is to facilitate conveying data from the point A (the source) to the point B (destination).
There are plenty of people, who, in plenty of cases, would prefer one approach (using DTOs) over another (using bare model classes/entities), and vice versa; however, there is no single source of truth?on which is better to use.
It very much depends on the requirements, software design, architectural approach you decide to stick with, other (project-related) specific details, and even on personal preference.
Some even claim that the?DTO is an anti-pattern; some love using them; some think, that data refinement/adjustment should happen on the consumer/client-side (for various reasons, out of which, one can be?No Policy for API changes).
That being said, YES, you can simply return the model class (in?Hibernate , it’s @Entity) instance (or list of instances) right from your controller and?there is no problem?with this approach. I would even say, that this does not even violate anything from?SOLID? or?Clean Code principles .
But again..
it depends on what do you use response for; what representation of data do you need; what should be the capacity and purpose of the object in question, and etc..
领英推荐
DTO is generally a good practice in the following scenarios:
Note, that you may have a situation, when your requirements involve both 1st (aggregation) and 2nd (modification/customisation) points mentioned above.
However, if you are sure, that your table/relation representations (instances of?@Entity classes ) are exactly what the client needs, there is no necessity of introducing DTOs.
Supporting further the idea, that @Entity can be returned to the presentation layer without DTO
You can reuse persistent classes outside the context of persistence, in unit tests or in the?presentation layer, for example. You can create instances in any runtime environment with the regular Java new operator, preserving testability and reusability;
2. Hibernate entities?do not need to be ?explicitly Serializable.
Senior Fullstack developer
1 年DTOs are horrible but easy to implement and you will se a fast result the first month. A much better approach i to read meta data about fields from database or have some sort och systems where meta data about fields in database are located. Implement one single transfer object for data that are able to dynamically allocate storage for the data it will transfer to or from server/client. A bit more dificullt to implement but you will have one single object used to transfer all data compared to hundreds (if the system is large) of DTOs.
Software Engineer @ Contour Software | .Net core | Microservices | REST APIs | Entity Framework | Sql Server | Flutter
2 年Thanks for sharing
Software Engineer
2 年So interesting, thank you Omar
Java Developer at CGI | Ex-TCS | Java 8 | Spring Boot | Hibernate | Angular 10 | Microservices | AWS | Docker | Jenkins | Devops | VSSUT, Burla, Odisha
2 年Thanks alot for this post...I was really confused in DTO. This article cleared my confusion.