5 Crossing the boundaries: Adapters, repositories, and the mapping problem
Completing the hexagon means connecting the application core to the outside world while keeping framework and delivery details out of it. Input adapters receive requests from sources such as HTTP, message queues, or command lines, translate them into commands or queries for the bus, and convert results into responses. They should translate rather than contain business logic; typed application errors can be mapped into protocol-specific responses, with exhaustive handling helping keep external contracts consistent.
Output adapters implement ports defined by the core, translating its requests into infrastructure operations and converting failures into application errors. Ports work best as role interfaces shaped by what the core needs, not header interfaces that mirror a particular technology. DTOs keep ordinary output-port contracts focused and prevent domain entities from leaking into external systems. Repositories are the exception: because they persist and reconstitute domain state, their core-owned contracts may accept and return domain entities. Their read and write methods can be separated according to consumer needs, while frameworks such as Spring Data remain hidden behind adapters.
Every boundary raises a mapping trade-off: fewer types reduce mapper work but increase coupling, while distinct types isolate concerns at the cost of more code to maintain. The approaches range from using one type everywhere, through one-way and two-way mapping, to a Full Bridge with separate request, application, domain, persistence, DTO, and response types. Two-way mapping—separating domain, persistence, and DTO models—is a practical default for many applications; the Full Bridge can be worthwhile when APIs and models must evolve independently, while simpler strategies may suit small or short-lived systems.
This chapter completes the outside ring of the hexagon. Input adapters translate external requests into commands and queries, output adapters implement the ports required by the core, and explicit mapping keeps each boundary separate.
Two input adapters, one use case. The REST controller and the message consumer both call the same command bus. The handler behind the bus is identical regardless of which adapter triggered it.
Error hierarchy to HTTP status code mapping. Each sealed subtype maps to a specific status code. The compiler enforces completeness—no subtype can be silently ignored.
The header interface on the left is shaped by the email implementation. The role interface on the right is shaped by what the application core needs, keeping the port technology-agnostic.
Spring Data reinterpretation. The domain-owned ArticleRepository at the top (annotated with the Shared Kernel's @Repository) defines the contract. The adapter in the middle implements that port and delegates to Spring Data at the bottom (annotated with Spring's @Repository). Two different annotations with the same name, two different layers, two different purposes.
The Full Bridge for a publication-readiness query. The request enters as raw HTTP input, becomes an application query, resolves to a validated domain identifier, loads the Article aggregate because the answer depends on domain rules, maps the domain result to a DTO, and finally maps that DTO to an HTTP response. No type crosses more than one boundary.
Summary
- Input adapters are translators that receive external stimuli—HTTP requests, messages, scheduled triggers—and convert them into commands or queries the bus understands. They map the Error hierarchy to protocol-specific responses using exhaustive switch on sealed types. Framework annotations belong here, not in the core.
- Output adapters implement port interfaces defined by the core, using whatever technology is appropriate. They translate between the port's domain-language DTOs and the infrastructure's native format. The core never sees the adapter—only the port.
- Role interfaces vs header interfaces determine whether ports protect the core or leak infrastructure. A role interface is shaped by what the core needs; a header interface mirrors the implementation. Ports belong to the application concern that needs them and use core-owned types, not presentation or infrastructure types. Driving and driven adapters translate between their external or infrastructure formats and the port contract. This is dependency inversion: the adapters depend on abstractions owned by the core. Design ports from the inside out.core; aPorts belong to the application concern that needs them and use core-owned types, not presentation or infrastructure types. Driving and driven adapters translate between their external or infrastructure formats and the port contract. This is dependency inversion: the adapters depend on abstractions owned by the core. Design
- DTOs are the currency of output port boundaries—lightweight, immutable records that carry exactly the data the port needs. Domain entities never cross output port boundaries directly. The one exception is repositories.
- Repositories are special output ports that work directly with domain entities. Their purpose—persisting and reconstituting domain state—requires the domain model to cross the boundary. The repository interface lives in the core, annotated with the Shared Kernel's @Repository. The implementation lives in the adapter layer, annotated with @Adapter.
- Repository interfaces distinguish writes from reads: save and delete for writes, read for loading domain entities into command handlers, and find for returning DTOs to query handlers. Keep methods together when they serve a cohesive responsibility; split the contract when unrelated consumers or reasons to change create unnecessary coupling. Use cohesion, Single Responsibility and Interface Segregation principles to guide that choice, but don’t create interfaces so narrow that they add indirection without reducing coupling. One adapter can implement several focused repository interfaces.distinguish writesreadsKeep methods together when they serve a cohesive responsibility; split the contract when unrelated consumers or reasons to change create unnecessary coupling. Use cohesion, Single Responsibility and Interface Segregation principles to guide that choice, but don’t create interfaces so narrow that they add indirection without reducing coupling. One adapter can implement several focused repository interfaces.
- Spring Data belongs inside the adapter, never on domain-owned ports. The SpringArticleRepository extends ListCrudRepository operating on a persistence entity, hidden behind the JpaArticleRepository adapter that the core depends on.
- Two @Repository annotations with the same name, two different layers, two different purposes.
- In WriteFlow, these boundaries complete the outside ring of the hexagon: REST controllers drive article use cases, repository adapters persist and reload articles, notification adapters deliver publication-related messages, and mappers keep each external representation separate from the domain model.
- The mapping problem arises at every boundary crossing. Four strategies exist along the convenience-isolation spectrum: no mapping (anti-pattern), one-way (API decoupled), two-way (API and persistence decoupled), and the Full Bridge (complete isolation at every boundary). Two-way is the pragmatic default. The Full Bridge is the right choice for public APIs, large teams, and long-lived systems. The only incorrect choice is no mapping at all.
FAQ
What are input adapters, and what responsibilities do they have?
Input adapters receive external requests, translate them into commands or queries for the application bus, and translate results into the delivery mechanism’s response format. They do not contain business logic.Can different input adapters drive the same use case?
Yes. For example, a REST controller, message consumer, and command-line interface can each send requests through the bus to the same use case. The delivery mechanism changes, but the application core does not.How should application errors be mapped to HTTP responses?
The REST adapter maps each application error type to an appropriate HTTP status and response body—for example, validation errors to 400, missing resources to 404, and conflicts to 409. The core returns its own error types and does not depend on HTTP or framework types.What is an output adapter, and what does it do?
An output adapter implements a port defined by the application core and translates between the port’s contract and an external technology, such as a database, email service, or API. It delegates technical work and does not make business decisions.What is the difference between a role interface and a header interface?
A role interface is shaped by what the application core needs from a collaborator. A header interface mirrors the implementing technology’s API and can expose infrastructure-specific concepts. Ports should generally be role interfaces owned by the core.Why are repositories treated as a special kind of output port?
Repositories persist and reconstitute domain state, so their contracts may accept and return domain entities. Other output ports generally use DTOs. The repository implementation still belongs outside the core and translates between domain entities and persistence types.How can repository contracts separate read-side and write-side needs?
Write methods such assave and delete support command handlers. Methods such as readById return domain entities when a handler needs to mutate an aggregate. Query-oriented methods such as findById can return DTOs or projections. Interfaces can be split when consumers need different subsets of these methods.
Clean Applications with Hexagonal Architecture ebook for free