TL;DR
A Data Transfer Object (DTO) is a lightweight programming pattern used to move only the necessary data between systems — like an API and a server — without exposing sensitive information. DTOs belong in object-oriented programming environments (Python, C++, Java) and should be simple, readable, and free of business logic. They are a practical tool for developers who need to control exactly what data gets shared during a system call.
What is a DTO?
A data transfer object (DTO) is an object that carries data between processes. You can use this technique to facilitate communication between two systems (like an Application Programming Interface (API) and your server) without potentially exposing sensitive information.
DTOs are commonsense solutions for people with programming backgrounds. If you're a developer, you probably know what these things are and how they work. If you're an average user, DTOs can be incredibly confusing. You'll use DTOs in object-oriented programming (OOP) language environments such as Python, C++, and Java. DTOs are easy to develop and update.
Why do calls create data exposure risks?
OOP environments rely on a system of "calls." Each one is a bit like a data lookup, and they require both time and processing speed. If you're not careful, calls can also expose sensitive data you'd like to keep hidden, such as:
- Employee addresses
- Account numbers
- Social Security numbers
- Business logins
How does a DTO limit what data gets shared?
As one programmer explains, you might require an employee's name and photo to enter your company. You need to provide that data for a match, but you don't need to give other information about the employee that you have within your database. A DTO can transfer only the information required.
How do you put a DTO to use?
A DTO should just contain data, not business logic. It's a simple, small thing that should do one task only.
A good DTO will:
- Minimize boilerplate. You'll write each one fresh.
- Be easy to create. DTOs shouldn't be so complicated that you struggle to write them. (Code like this is easy to break.)
- Be readable. Anyone should be able to parse your code.
We talked a bit about APIs in this article. If you're not sure what the acronym means or how APIs could help you, check out our blog post.
Frequently asked questions
What does Data Transfer Object (DTO) stand for?
DTO stands for Data Transfer Object — a programming construct designed to carry data between processes or systems without exposing unnecessary or sensitive information.
What programming environments use Data Transfer Objects (DTOs) ?
DTOs are used in object-oriented programming (OOP) environments, including languages such as Python, C++, and Java.
What kinds of sensitive data can a Data Transfer Object (DTO) help protect?
DTOs can help prevent the unintended exposure of sensitive data such as employee addresses, account numbers, Social Security numbers, and business login credentials during system calls.
Should a Data Transfer Object (DTO) contain business logic?
No. A DTO should contain only data, not business logic. It is intended to be a simple, single-purpose object focused solely on transferring data.
What makes a well-written Data Transfer Object (DTO)?
A good DTO minimizes boilerplate code, is straightforward to create, and is readable by anyone reviewing the code. Overly complex DTOs are harder to maintain and more prone to errors.
How does a Data Transfer Object (DTO) differ from a regular API call?
While a standard API call may return all available data from a database, a DTO allows developers to specify and transfer only the exact data fields needed for a given interaction, reducing exposure and improving efficiency.
References
- Data Transfer Object Pattern in Java: Implementation and Mapping. Stack Abuse.
- Rethinking the Java DTO. (January 2020). Scott Logic.
- Logistipedia.