Topic 052 / Advanced
Hexagonal architecture
Chapter 4.4 · Dependencies and Application Structure
What and why
Clean architecture (→ 51) describes rings and a dependency rule, but has
no vocabulary for "the thing the domain depends on" versus "the thing that plugs into
it" — a vocabulary that matters once a domain has many kinds of outside connections (HTTP
in, a database out, a queue in and out, a second UI). Hexagonal architecture (ports
and adapters) names the domain's required interfaces ports (e.g. a SaveTask port, a
NotifyUser port) and the concrete implementations that plug into them adapters (e.g.
a Postgres adapter, an SMTP adapter). A port can be driving (something outside calls
into the domain, like an HTTP adapter) or driven (the domain calls out through it,
like a repository). Builds on: → 51.
Diagram
Driving adapters Driven adapters
(call INTO the domain) (domain calls OUT through these)
HTTP API ──┐ ┌── PostgresTaskRepository
CLI ──┼──► [ Ports ] ──► Domain ──► [ Ports ] ────────┼── SmtpNotifier
Scheduler ──┘ └── InMemoryFake (tests)
The domain does not know whether it's called by HTTP or a cron job, and does not
know whether its repository port is backed by Postgres or a fake.
How it works
- Identify every point where the domain talks to the outside world, in either direction.
- For each, define a port — an interface named from the domain's point of view
(
ReminderNotifier, notSmtpClient). - Build a driving adapter for each way the outside world invokes the domain (a REST controller, a CLI command, a scheduled job); each translates its input into a call on the application layer.
- Build a driven adapter for each way the domain reaches outward (a database repository, an email sender); each implements a port the domain defined.
- Wire adapters to ports at startup; the same domain accepts a REST adapter today and a CLI adapter tomorrow with no domain change, and swaps a real driven adapter for a test fake with no domain change either.
Practical example
(illustrative). In the task manager, SaveTask and
ReminderNotifier are ports; a REST controller and a scheduled-reminder job are two
different driving adapters calling the same use case; PostgresTaskRepository and an
in-memory fake are two driven adapters behind the same SaveTask port — tests run the
whole domain against the fake with no database at all.
When to use it
- Use when: a domain is genuinely invoked from more than one direction (API, CLI, and scheduled jobs) or needs fast, database-free tests.
- Avoid when: there is exactly one way in and one way out, with no near-term plan for a second — the port/adapter vocabulary around a single implementation each is the same over-abstraction → 43 and → 44 already warn against.
- Failure scenarios: a port defined too close to one adapter's shape (a method taking a SQL row, say) so a second adapter cannot implement it without changing the port — the abstraction has leaked.
- Simpler alternative: clean architecture's plain rings (→ 51), or layered architecture (→ 50), when only one inbound and one outbound integration exist.
Practice
(20–30 min; no lab services — any language). Define a ReminderNotifier
port for the task manager; implement two driven adapters — one that prints to the
console (standing in for SMTP) and a fake that records calls in memory — and run the
same use case against both without changing the use case's code. Done when both adapters
pass the same use-case run with zero changes to the domain code.
Key takeaway
Ports are domain-owned interfaces; adapters are the concrete things that plug into them, in either direction. Done when you can: swap a driven adapter in a test without touching a line of domain code. Used next in → 53 (a modular monolith can give each module its own ports-and-adapters boundary).
References
None — this topic makes no external claims.
Source directory