liveBook · Manning

Data transfer object

As explained in Microservices Patterns, Second Edition

A data transfer object is a data structure that carries structured data across an interface without exposing domain implementation objects.

A data transfer object (DTO) models a message exchanged between an API client and a service. It serializes outbound data into the service’s expected format and deserializes inbound data into application-usable domain objects. A client may model only the message types or fields relevant to its responsibilities. section 8.1.7

Choose an implementation strategy by weighing development effort, correctness, type safety, and design-time coupling:

  • Handwritten classes take more time but can reduce design-time coupling. section 8.4
  • Code-generated classes are convenient, but can couple the client to the entire API. Binary payloads are almost always handled with generated DTOs. section 8.1.7 section 8.4
  • Untyped maps or dictionaries are another option; service-provided libraries are also listed as an implementation strategy. section 8.1.7
Figure 8.3 An API client must have a data model of the messages that it exchanges with the service. The data model must serialize and deserialize correctly as defined by the schema. It can either be untyped (e.g. hash maps) or consist of handwritten or code-generated classes.
defining api client dtos

Shows how an API client’s message model can use untyped structures, handwritten classes, or generated classes, while conforming to the schema for serialization and deserialization. section 8.1.7

How Microservices Patterns, Second Edition covers it

How deep it goes
Compares untyped maps, handwritten classes, code generation, and service-provided libraries against effort, correctness, type safety, and coupling.
Examples
Uses requests, commands, events, responses, replies, consumed events, and map-based message models as examples.
What you can do after
Build it from scratch by selecting handwritten, generated, or untyped DTOs for an API client's message payloads.
How it connects
Connects API clients with message serialization, deserialization, schemas, and code generation.

Also explained in

Compare how 10 books explain data transfer object →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.