What is a DTO?

15

I'm messing around with Java a short time and I always hear the DTO expression related to pulling something out of a bank, but I've never quite understood what it's all about.

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 it?

    
asked by anonymous 03.09.2014 / 17:31

1 answer

32

Data Transfer Object (DTO) or simply Transfer Object is a well- for the transport of 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. In the same way it would be inefficient or even cause errors to pass a more complex entity.

In addition, the data used in the communication often does not accurately reflect the attributes of your model. So a DTO would be a class that provides exactly what is needed for a particular process.

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

When I have a well-structured domain, I prefer to create beans that represent this template. These beans are usually called Entities . There, in certain cases (such as certain queries in the views database or having joins ), I create an TO or DTO type to facilitate the transport of this data .

References:

03.09.2014 / 18:02