Keep business rules in the middle and plug the outside world in through ports and adapters.
Hexagonal architecture (also called Ports and Adapters) puts your business rules in the middle and keeps every tool (database, web framework, message broker, other APIs) outside, behind small plugs.
Alistair Cockburn introduced this idea in 2005. The aim is simple: keep the important code free from technical details. In many apps the rules ("a customer cannot order more than the stock") are mixed with SQL queries and HTTP code. Then a change in the database or the web framework forces changes in the rules.
In a hexagonal design the application has an inside and an outside. The inside is the core: the domain model (orders, prices, rules) and the use cases ("place an order"). The outside is everything else. They talk through ports.
| Part | What it is | Example |
|---|---|---|
| Domain model | Objects and rules of the business, with no outside imports | Order, Money, Stock, rule: stock cannot go below zero |
| Use case (application service) | One thing a user wants to do. It orchestrates the domain and ports | PlaceOrder, CancelOrder |
| Driving port | Interface the outside uses to start a use case | interface PlaceOrder { execute(cmd) } |
| Driven port | Interface the core needs from the outside, owned by the core | interface OrderRepository { save(order) } |
| Driving adapter | Turns an outside request into a call on a driving port | REST controller, CLI command, queue consumer, test |
| Driven adapter | Implements a driven port with a real tool | PostgresOrderRepository, SmtpEmailSender, StripePayments |
A simplified picture. In a real project the numbers depend on how the code is split, but the direction is always the same: ports keep the rules free from tools.
| Term | Meaning in easy words |
|---|---|
| Core / domain | The business rules and use cases, free from tools |
| Port | An interface that defines how the core talks to the outside |
| Adapter | Code that connects a port to a real tool or caller |
| Driving (primary) side | Callers that start the core: UI, REST, CLI, tests |
| Driven (secondary) side | Things the core calls: database, email, other APIs |
| Use case | One action a user wants to do, such as Place Order |
| Dependency inversion | High-level code owns the interface, low-level code implements it |
| Dependency injection | Passing the adapter into the core from outside, instead of the core creating it |
| Fake / in-memory adapter | A simple stand-in that makes tests fast |
| Anti-corruption layer | An adapter that translates a messy outside model into your clean one |
| Composition root | The one place where adapters are created and connected |
| Choice | We gain | We lose |
|---|---|---|
| A port for every tool | Full freedom to swap and fake | More boilerplate and more things to name |
| Few ports, only where needed | Less code | Some tools stay tied to the core |
| Separate domain and database models | A clean domain, free from table shapes | Mapping code in both directions |
| Shared model for both | Less code | The core is shaped by the database again |
| Thin adapters | Rules stay in one place | Need discipline in code reviews |
| Strict dependency rules (tool-checked) | The structure cannot drift | Setup time and some rigidity |