chapter five

5 Crossing the boundaries: Adapters, repositories, and the mapping problem

 

This chapter covers

  • Implementing input adapters that drive use cases through the bus
  • Mapping application errors to protocol-specific responses
  • Designing output adapters that implement core-owned ports
  • Distinguishing role interfaces from header interfaces
  • Understanding repositories as privileged output ports
  • Splitting repository contracts by consumer need
  • Wrapping data frameworks behind domain-owned ports
  • Choosing a mapping strategy for each boundary

We built the inside of the hexagon: use cases expressed as honest contracts, commands and queries that validate themselves, handlers that orchestrate without owning business logic, and a bus that routes it all. The core is complete. It also has no way to talk to the outside world. That is what this chapter fixes.

For WriteFlow, this means turning the application core from a set of isolated use cases into something the outside world can actually use: publishing articles through HTTP, persisting article state, sending publication notifications, and returning stable responses without exposing internal domain objects.

5.1 Input adapters: Driving the hexagon

5.1.1 What an input adapter is (and is not)

5.1.2 Error mapping: from Error hierarchy to HTTP responses

5.2 Output adapters: Serving the hexagon

5.2.1 What an output adapter is (and is not)

5.2.2 Role interfaces versus header interfaces

5.2.3 Implementing an external service adapter

5.2.4 DTOs as the currency of the boundary

5.3 Repositories: A privileged kind of output port

5.3.1 Why repositories are different: Domain entities cross the boundary

5.3.2 Repository interface design: read-side vs write-side

5.3.3 Spring Data reinterpretation: Wrapping the framework

5.3.4 Persistence mapping in practice

5.4 The mapping problem: A journey toward full isolation

5.4.1 Why mapping exists: The boundary tax

5.4.2 Strategy 1: No mapping

5.4.3 Strategy 2: One-way mapping

5.4.4 Strategy 3: Two-way mapping

5.4.5 Strategy 4: The full bridge

5.4.6 Choosing a strategy: Trade-offs at a glance

5.5 Summary