Difference between Entity and DTO & What is the use of DTO instead of Entity?
Omar Ismail
Senior Software Engineer @ Digitinary | Java & Spring Expert ? | AWS & Microservices Architect ? | FinTech & Open Banking Innovator ?? | Digital Payments Expert ?? | Top 200 IT Content Creator in Jordan ?? | 40K+ ??
Short answer:
Long answer:
While the term "Data Transfer Object" (DTO) is defined quite unambiguously, the term "Entity" is interpreted differently in various contexts.
The most relevant interpretations of the term "Entity", in my opinion, are the following three:
The Jee- and Jpa-community sees entities primarily as objects mapped to a database table. This point of view is very close to the definition of a DTO - and that's where much of the confusion probably stems from.
In the context of domain-driven design, as well as Robert Martins's point of view, however, Entities are part of a business domain and thus can and should implement behavior.
What is the use of DTO instead of Entity?
What is the use of DTO instead of Entity?
All examples will be based on this simple data model:
A?Person?entity has five properties:?Id, FirstName, LastName, Age, CityId
And you can assume that the application uses this data in many varied ways (reports, forms, popups, ...).
The entire application already exists. Everything I mention is a change to the existing codebase. This is important to remember.
Example 1 - Changing the underlying data structure - Without DTO
The requirements have changed. The age of the person needs to be dynamically retrieved from the government's database (let's assume based on their first and last name).
Since you don't need to store the?Age?value locally anymore, it, therefore, needs to be removed from the?Person?entity. It's important here to realize that the entity represents?the database data, and nothing more. If it's not in the database, it's not in the entity.
When you retrieve the age from the government's web service, that will be stored in a different object (or int).
But your front end still displays an age. All the views have been set up to use the?Person. Age?property, which now no longer exists. A problem presents itself:?All views that refer to the?Age?of a person need to be fixed.
Example 2 - Changing the underlying data structure - With DTO
In the old system, there is also?a PersonDTO?entity with the same five properties:?Id, FirstName, LastName, Age, CityId. After retrieving a?Person, the service layer converts it to a?PersonDTO?and then returns it.
But now, the requirements have changed. The age of the person needs to be dynamically retrieved from the government's database (let's assume based on their first and last name).
Since you don't need to store the?Age?value locally anymore, it, therefore, needs to be removed from the?Person?entity. It's important here to realize that the entity represents?the database data, and nothing more. If it's not in the database, it's not in the entity.
领英推荐
However, since you have an intermediary?PersonDTO, it's important to see that this class can?keep?the?Age?property. The service layer will fetch the?Person, convert it to a?PersonDTO, it will then also fetch the person's age from the government's web service, will store that value in?PersonDTO.Age, and passes that object.
The important part here is that?anyone who uses the service layer doesn't see a difference between the old and the new system. This includes your frontend. In the old system, it received a full?PersonDTO?object. And in the new system, it still receives a full?PersonDTO?object.?The views do not need to be updated.
This is what we mean when we use the phrase?separation of concerns: There are two different concerns (storing the data in the database, presenting data to the frontend) and they need a different data type each. Even if those two data types happen to contain the same data right now, that might change in the future.
In the given example,?Age?is a difference between the two data types:?Person?(the database entity) doesn't need an?Age, but?PersonDTO?(the frontend data type) does need it.
By separating the concerns (= creating separate data types) from the beginning, the codebase is much more resilient to changes made to the data model.
You might argue that having a DTO object, when a new column is added to the database, means you have to do double work, adding the property in both the entity and the DTO. That is?technically?correct. It requires a bit of extra effort to maintain two classes instead of one.
However, you need to compare the effort required. When one or more new columns are added, copy/pasting a few properties doesn't take all that long. When the data model changes structurally, having to change the frontend, possibly in ways that only cause bugs at runtime (and not at compile time), takes considerably more effort, and it requires the developer(s) to go hunting for bugs.
I could give you more examples but the principle will always be the same.
To summarize
As a rule of thumb to consider separating concerns, think of it this way:
Suppose that every concern (the UI, the database, the logic) is handled by a different person in a different location. They can only communicate by email.
In a well-separated codebase, a change to a particular concern will only need to be handled by one person:
If all of these developers were using the same?Person?entity, and a minor change was made to the entity,?everyone?would need to be involved in the process.
But by using separate data classes for every layer, that issue isn't as prevalent:
The key phrase here is?since it doesn't affect them. Implementing a good separation of concerns seeks to minimize affecting (and therefore having to involve) other parties.
Of course, some major changes can't avoid including more than one person, e.g. when an entirely new entity is added to the database. But don't underestimate the number of minor changes that you have to make during an application's lifetime. Major changes are a numerical minority.
RevOps & CRM Architect | Help organizations architecting RevOps driven by automation, integration & data strategy
2 年Wow clear explanation! Help me a lot understanding Java structure, thanks!
IT Consultant, Software Architect, Professor UPC, expertise: Microservices, JavaEE, Spring, Weblogic, IBM, Data Science.
2 年Vulnerability?
Développeur back-end Java/Springboot
2 年Thank you for this clear but complete explanation ??