Entity vs Dto vs Model

2 minute read

Several times I take on legacy or somewhat old projects in my daily life, which those who developed it had the same doubt that I already had: is it an entity, a dto or a model? And ended up making the same “mistake” that I already made, just choosing dto or entity to name the folder in the dto and mixing all the classes in that same folder 😅 It turns out that we ended up missing some details like that early in our career.

Entity

In different contexts, an entity can represent something different, let’s focus on .NET only: It is a class that represents an object in the database. For example, a customer table mapped using Column attributes:

[Table("customers")]
public class Customers {
    [Table("id")]
    public long Id { get; set; }
    [Table("first_name")]
    public string FirstName { get; set; }
    [Table("last_name")]
    public string LastName { get; set; }
    [Table("age")]
    public int Age { get; set; }
}

Note that all fields are linked to a column of the object in the database, this is the ideal scenario for an entity, it uniquely and completely represents an object in the database, no business rules, no functions, just that.

Dto (Data Transfer Object)

According to Omar Ismail - The Jee and Jpa community sees entities primarily as objects mapped to a database table. This view is very close to the definition of a DTO - which is where much of the confusion probably comes from DTOs are data sent and received between clients and the server. Why not use the same class for both purposes? After all, I could just fetch the record from the database and return to the front-end. When you make a change to this class, you will need to change the front-end as well. When you use a DTO for the transfer, you will only need to change the front-end if really necessary, in addition to not having to return anything more than necessary, so you will have less people (less work, time and money) involved for a small amount of time. change in the structure of the database.

Example: You have a varchar(30) field in a sales table that represents the source of sales (eBay, Telephone, Ecommerce), and you realize that it would be better to just save an id to another table with this information: If you return a DTO , there will be no change to it, just your query, so the front-end developer won’t need to change anything.

Model

A model represents a real-world object that is related to the problem or domain space. In other words, it’s where you place your business rules, functions, methods, etc… But don’t confuse it with the ViewModel, which is an intermediary for the view, containing the Models needed to be displayed in a View, as well as capturing events from the View. view(Click, KeyDown…). For example, a class that you use to encrypt any text is a Model; A class used to calculate mathematical functions, is also a Model.

Summary

  • Entity: Database object
  • DTO: Object you transfer between client/server
  • Model: Class where you place your methods/functions and/or that is related to the problem that the application solves, normally it is what you will use if it is not Entity or DTO.

Sources: 1, 2, 3, 4

Updated: