Problem
Business logic becomes entangled with web frameworks, databases, and message brokers, so it can only be tested through them and changing a technology means rewriting rules. Teams struggle to run fast tests or swap infrastructure.
Also known as: ports-and-adapters, hexagonal-pattern
Isolate application core logic from delivery mechanisms and infrastructure by communicating only through ports implemented by adapters.
Business logic becomes entangled with web frameworks, databases, and message brokers, so it can only be tested through them and changing a technology means rewriting rules. Teams struggle to run fast tests or swap infrastructure.
Hexagonal architecture, introduced by Alistair Cockburn, treats the application as a core surrounded by ports and adapters. The shape is not literally six-sided; it simply shows that many different actors can touch the core from any side. The core knows nothing about HTTP, SQL, or queues.
Inbound adapters translate external requests into calls on a driving port (a use case interface). The core executes domain logic and calls driven ports for anything external. Outbound adapters implement those ports.
[REST adapter] --> (OrderService port)
|
[ Domain core ]
|
(OrderRepository port) --> [Postgres adapter]
(PaymentGateway port) --> [Stripe adapter]
interface OrderRepository { Order find(OrderId id); void save(Order o); }
class PlaceOrder { // use case in the core
PlaceOrder(OrderRepository repo, PaymentGateway pay) {...}
}
In tests you pass an in-memory repository and a fake gateway. In production the composition root wires the real adapters.
None — structural pattern
Low — no runtime footprint
Medium — dependency inversion discipline
Ports that mirror database tables, leaking persistence into the core
Adapters containing business rules that bypass the core
One adapter per port with no real variation, producing ceremony only