NextGen APIs

Payload via DataTransferObject (DTO)

A data transfer object (DTO) is an object that carries data between processes to reduce the number of methods calls. Even though the main purpose of the DTO pattern is to batch up what would be multiple remote calls into a single call, it is worth mentioning that another advantage is to encapsulate the serialisation mechanism for transferring data over the wire.

A DTO is usually created as a POJO. They are flat data structures that contain no business logic. They only contain storage, accessors and eventually methods related to serialisation or parsing. The data is mapped from the domain models to the DTOs, normally through a mapper component in the presentation or facade layer.

The DTO represents the model sent from or received to the API, thus it either packs together the request sent to the server or optimises the response of the client.

The DTO pattern is also useful to facilitate communication between two systems without potentially exposing sensitive information, since it only allows to publish the needed information, the scope of data is defined by the DTO. By using a DTO to transfer just the required information, the coupling between the API and the model is loosened, allowing for easier maintenance and scaling of the service.