Hexagonal Architecture (Ports and Adapters)
Isolate the domain logic from the outside world.
“Allow an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases.” – Alistair Cockburn
Domain-Centric.
graph TD
subgraph Core [Domain Core]
Logic
end
subgraph Ports [Ports/Interfaces]
Service -->|Uses| RepoInterface
Driver -->|Calls| ServiceInterface
end
subgraph Adapters [Adapters]
WebController -->|Calls| ServiceInterface
RepoImpl -->|Implements| RepoInterface
end
When to use
- Long-lived applications.
- Domain-Driven Design (DDD).
- Complex Business Logic.
Explanation
- Core: Pure logic. No dependencies on DB or Web.
- Ports: Interfaces defining how to talk to the core (Inbound) and how the core talks out (Outbound).
- Adapters: Implementations (Rest API, Postgres DB, CLI).
Pros and Cons
| Pros | Cons |
|---|---|
Code example
Typescript