Data Transfer Object DTO Definition and Usage

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 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.

We'll do our best to explain DTOs in plain language. But know that we may need to get a bit technical to highlight how they work.

What is a DTO?

As we explained, DTO stands for data transfer object. As the name suggests, a DTO is an object made to transfer data.

You'll use DTOs in object-oriented programming (OOP) language environments such as Python, C++, and Java. DTOs are easy to develop and update.

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

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.

dto Sketch image

How to 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:

  • Minimise 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.

References

Data Transfer Object Pattern in Java: Implementation and Mapping. Stack Abuse.

Rethinking the Java DTO. (January 2020). Scott Logic.

Logistipedia.