Stop using Data Transfer Objects (DTOs)

Data Transfer Objects (DTOs) have been a common practice in software development for years

Enver Daniel Francisco Báez
3 min readMar 17, 2023

They are objects used to transfer data between different layers of an application, such as from the frontend to the backend or between different microservices. DTOs are intended to provide a simple and standardized way to transfer data and can help to decouple different layers of an application. However, DTOs can also introduce unnecessary complexity, make the code harder to maintain, and create performance issues.

In this article, we’ll explore the reasons why you should stop using DTOs in your code and suggest some alternatives.

What are DTOs?

DTOs are simple objects that contain data, usually in the form of fields or properties, without any behavior or business logic. DTOs are used to transfer data between different parts of an application, such as between the frontend and backend or between different microservices. DTOs typically mirror the structure of the data they are transferring, and they can be used to encapsulate multiple pieces of data into a single object.

DTO of Person

Why you should stop using DTOs

While DTOs can be useful in some cases, they also have several drawbacks that make them problematic in many situations.

1. Unnecessary complexity

DTOs can introduce unnecessary complexity into an application. They require developers to create additional objects and maintain mapping code to convert data from one object to another. This can make the code harder to understand and maintain.

2. Code duplication

DTOs often mirror the structure of the data they are transferring, which can lead to code duplication. For example, if you have a DTO for a user object and a domain object for a user, you will need to maintain two separate objects that have the same structure.

3. Performance issues

DTOs can create performance issues because they require additional mapping code to convert data from one object to another. This can increase the amount of processing that needs to be done and slow down the application.

4. Breaking encapsulation

DTOs can break encapsulation because they require exposing data outside the domain layer. This can lead to issues with data consistency and security.

Alternatives to DTOs

There are several alternatives to DTOs that can help to address the issues mentioned above.

1. Domain objects

Domain objects are objects that represent the business logic and behavior of an application. They can be used to encapsulate data and behavior, making the code more maintainable and easier to understand.

2. View models

View models are objects that represent the data that is being displayed to the user. They can be used to encapsulate data and behavior related to the presentation layer of an application.

3. Service contracts

Service contracts are interfaces that define the methods and parameters that are exposed by a service. They can be used to define the communication between different parts of an application without the need for DTOs.

Conclusion

While DTOs have been a common practice in software development for years, they also have many drawbacks that make them problematic in many situations. Unnecessary complexity, code duplication, performance issues, and breaking encapsulation are just a few of the issues associated with DTOs. There are numerous alternatives to DTOs, such as domain objects, view models, and service contracts, that can help to address these issues and make the code more maintainable and easier to understand. So, next time you are tempted to use a DTO in your code, consider whether there is a better alternative that can help to simplify your code and make it more maintainable.

--

--