One thing I used to do in my first projects was using the base model to receive and send data.
Of course, since my first projects were very small, I didn't really feel the need to use DTOs. However, one day, while studying this topic, I realized that using DTOs (Data Transfer Objects) is generally a better approach than using the base model directly.
And there is more to it than just efficiency when working with DTOs.
Some advantages are:
- Performance: One of the main benefits of DTOs is sending the right amount of data instead of the entire object. You can also combine them with specific database queries to avoid retrieving unnecessary information.
- Extra security: It is not a good idea to expose your base models directly to users. Doing so might expose information they shouldn't have access to and create security risks, especially when receiving data from external requests.
- Flexibility: You can have DTOs that aren't directly connected to any base model. They can exist just to send specific information that doesn't necessarily need an entire object behind it.
Examples
Let's imagine we have the following model:
public class Question
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Topic { get; set; }
public string Description { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
Here are some DTOs based on the model above.
//CreateQuestionDto
//There is no reason to receive the dates or the ID from the user when creating a question. The backend handles those values.
public class CreateQuestionDto
{
public string Title { get; set; }
public string Topic { get; set; }
public string Description { get; set; }
}
//ReturnQuestionDto
//This DTO contains the ID but doesn't include the dates, since they aren't necessary for this particular response.
public class ReturnQuestionDto
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Topic { get; set; }
public string Description { get; set; }
}
//ReturnListQuestionDto
//This one is useful when returning a list of questions. Instead of sending all the information about every question, we can use a lightweight DTO containing only what is necessary for the list.
public class ReturnListQuestionDto
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Topic { get; set; }
}
//QuantityOfQuestionPerTopicDto
//This is an example of a DTO that isn't directly based on the Question model, its purpose is to return information about how many questions exist in each topic, without sending all the questions at once.
public class QuantityOfQuestionPerTopicDto
{
public string Topic { get; set; }
public int Quantity { get; set; }
}
Notice how each DTO has a different purpose, even though they are all related to the same Question model.
These are just a few examples that can help you understand how to use DTOs in your projects.
AND ATTENTION: Yes, AI can generate DTOs for you, but you still need to understand their structure so you can identify potential flaws and unnecessary properties. AI isn't perfect, and blindly accepting generated code can create problems.
DTOs can also make frontend development easier because you don't need to accommodate unnecessary data or deal with properties that your components don't actually use.
The main idea is simple: don't send the entire object just because you can. Send what your application actually needs.
Top comments (0)