liveBook · Manning

Data mapping

As explained in Clean Applications with Hexagonal Architecture

Data mapping is the transformation of data between representations used by different system components or layers.

Reusing one type for an API response, domain entity, and persistence record couples those concerns: a local change can ripple across the system. Mapping keeps each representation separate so changes stay at the boundary where they belong. section 5.4.1

Each layer has different needs: persistence uses mutable ORM objects, the domain uses objects with behavior, and APIs need consumer-shaped serializable data. Mapping costs effort to write and maintain, but prevents one representation from dictating changes across layers. section 5.4.1

Choose the amount of mapping at each boundary:

  • No mapping couples representations; one-way mapping decouples the API. section 5.5
  • Two-way mapping decouples API and persistence and is a pragmatic default; the Full Bridge isolates every boundary. section 5.5

Translate a neighbor’s model at the boundary before it reaches a use case. An outbound adapter maps a service response to the DTO declared by the local output port; an input adapter maps an incoming contract to a command or query. Map remote failures to local errors there as well. section 11.4 section 11.8

Table 11.1 Mapping the author-service response into WriteFlow’s model
Before translation
After translation
Author-service response, in the author service’s model
DTO declared by WriteFlow’s output port, in WriteFlow’s model

Shows an author-service response translated into the DTO owned by the local output port. section 11.4

How Clean Applications with Hexagonal Architecture covers it

How deep it goes
Compares four mapping strategies and assigns translation to boundary adapters without prescribing one strategy for every system.
Examples
Uses an author-service response, a partner CMS request, and remote failures translated into local errors.
What you can do after
Apply it by translating each external model at the boundary before it reaches a use case, including mapping remote failures to local errors.
How it connects
Connects mapping to domain models, DTOs, persistence entities, ports and adapters, and anti-corruption layers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clean Applications with Hexagonal Architecture. The section links point to the parts of the book it draws on.