Data Transfer Object

Definition
An object that carries data between processes
Data Transfer Object

How does it work?

The purpose of such an object is to facilitate data transfer. To this end it contains serialization/deserialization methods (for JSON/XML) and nothing else. Its attributes are typically scalar and easy to serialize.

A DTO is neither an entity nor a value object, though it often contains the data of the entity and is immutable like a value object. The difference is that the data of the DTO is specially prepared for data transfer. The data is simple and the class contains no behaviour.

When should you use it?

  • When transporting objects over a network.

Problems

  • A DTO is an extra class and it only pays out to create a special DTO for an entity if the entity is too complicated to (de)serialize.

Links