Architectureports and adaptersdriving adapterdriven adaptertest double

Hexagonal architecture

Definition

Hexagonal architecture, or ports and adapters, isolates the application core behind ports: driving adapters (HTTP, queue consumers, tests) call the core, and driven adapters (database, payment provider) implement interfaces the core declares.

In practice

Every adapter depends on the core and the core depends on no adapter, so a new channel or technology is a new adapter rather than a change to business rules. The same use case can be driven from a REST endpoint, a message consumer and a unit test, and a driven adapter can be replaced by a test double while the real database is down.

Adapters still need their own integration tests, because a fake repository proves nothing about SQL. Too many fine-grained ports add ceremony, and ports shaped after one technology (IDbTransaction leaking into the core) defeat the idea. It shares the inward dependency direction of clean architecture.

Example

The core defines IPaymentPort; production uses PspPaymentAdapter, tests use FakePaymentAdapter that approves amounts under 100 EUR, and both HTTP and a RabbitMQ consumer drive PlaceOrder.