What is a DTO?

Asked

Viewed 46,121 times

30

I’ve been dealing with Java for a little while and I always hear the expression DTO related to pulling something from a bank, but I never really understood what it is.

Is it just a name to reference the process of pulling information from a database? Or a pattern of access to information in the tables? What exactly is?

1 answer

63


Data Transfer Object (DTO) or simply Transfer Object is a widely used design standard in Java for transporting data between different components of a system, different instances or processes of a distributed system or different systems via serialization.

The idea is basically to group a set of attributes into a simple class in order to optimize communication.

In a remote call, it would be inefficient to pass each attribute individually. Similarly it would be inefficient or even cause errors to pass a more complex entity.

Also, often the data used in communication does not exactly reflect the attributes of your model. So, a DTO would be a class that provides exactly what is necessary for a given process.

In some cases, use the DTO or TO to map information obtained from the database and then use in a View (MVC). This is not completely wrong, and can even optimize the presentation of the data, after all the Controller already gets the ready-to-use information. However, this can also end up in a very polluted model with redundant information.

When I have a well structured domain, I prefer to create Beans representing this model. These Beans are usually called Entities. There, in certain cases (as in certain database searches based on views or who possess joins ), create a type of OT or DTO to facilitate the transport of such data.

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.