Context:
I was reading in different forums that the DTO (data transfer object) is useful to show or return the information that we need expressed through a DTO object that contains part or all of the attributes that a certain entity has. The question is whether when building an object it is necessary to use a DTO object with the same attributes as an entity only for this purpose? Example:
We have an entity:
public Casa{
private List<Habitaciones> habitaciones;
private String domicilio;
}
We have a DTO:
public CasaDTO{
private List<Habitaciones> habitaciones;
private String domicilio;
}
In this example we only change the name of the class, in this way the object can be built using both models and there would be no difference. So: What is the use of having an entity, if the DTO can already build the house? or vice versa.
Continuing with the same example, in the case if we had to show the perimeter of the house, which is a calculated data, so I am not sure if it would be correct to add an attribute in the CasaDTO class that is private double perimetro;
, although in case of adding it, At least there I understand that there is a difference between a DTO and an entity, since the models would share some data but there would be at least one different attribute, and we could use the DTO to show or return the information of the perimeter of the house.
Following this line, from my understanding, for it to make sense to create a House model, I would use it to build a House and use CasaDTO to show the house with the perimeter, otherwise I don't see why creating a House entity makes sense; At the same time, having seen the code of different people, I see that many times the entity models and the DTO's usually have the same attributes, I would like to understand the reason for this, what would be the correct path to follow according to good practices and in what cases or what an entity that has the same attributes as a DTO class would be useful for.
An entity is an object that maps to a table in a relational database model, this entity has only properties that represent the columns of the table it is mapping to. In other words, an entity maps a data structure (table) from the relational database model to an object-oriented model, where the tables are represented as classes and each row or tuple of that table would be an object in the world of the database. Object-oriented programming. Entity classes have strictly the same number of properties as the table they are mapping to, and they do not manage logic, that is, they do not have methods (functions or procedures), only properties.
Now, the DTO pattern allows us to create structures independent of our data model and, just as entity classes only have properties, they do not have any logic.
Now suppose we have the Author and Book entity classes , where the relationship could be established by saying that an author has written many books, but a book has been written by only one author, that is, we have a one-to-many relationship, where the extreme one would be the author and the lot the books.
The structure of the Author entity class could be:
An author has a name, a country, an age and the list of books he has written.
The structure of the Book entity class could be:
A book has a title, an ISBN (it is the identification number of the book), genre (police, adventure, etc.) and the author who wrote it.
And suppose we want to make a method that returns only the titles of the books whose genre is adventure and the name of the author, that is, that supposed query would only return a list of objects, where each object would have data from the two classes of entity, that is, data from both classes would be combined and returned as properties of a single object, from the Book entity class we will only obtain the title and from the Author entity class only the name of the author.
So what would be the data type for the objects that would be returned in that query. Can it be of type Book or type Author ?
As you will see, it cannot be of any of these types, since Book has 4 properties and Author also has 4 properties , and our query will return an object that has 2 properties of type string ( AuthorName and Title ).
So since it cannot be the type of any of the entity classes we have, we have to create a new class with this structure of two properties of type string , this is the DTO pattern . The new class could well be:
You can also see this answer that also deals with the topic of DTO classes so that you have a better idea of the concept and why to apply it:
Pass a C# linq query from a button to a class to execute it and fill a grid in a form