Bookmarks

Clean architecture

A way of organising code in which business rules sit at the centre and all dependencies — on interfaces, the database and frameworks — point towards them, not the other way round.

checked 8 Nov

All terms

The idea is to separate what the business pays for — domain rules — from details: the web framework, the database, external APIs. Details can be changed or rewritten without touching the rules. This is achieved with a single rule: source code may only depend inwards, towards the business rules.

The dependency rule and the circles

CircleWhat it containsDepends on
Entitiesenterprise business rules: domain objects and termsnothing
Use casesapplication rules: how entities are used, input and output portsentities only
Interface adapterscontrollers, presenters, gateways: converting data between the formats of the circlesuse cases and entities
Frameworks and driversweb framework, database, user interface, external servicesadapters

Dependencies cross circle boundaries only inwards. Use case code does not know what is outside — PostgreSQL or a file, REST or GraphQL; the outer layer knows about the inner one, not the other way round. Where control flows outwards, the boundary is crossed through an interface: it is declared inside and implemented outside, so the direction of dependencies does not change. Frameworks are therefore kept at the edge: your code calls the framework, not the other way round.

What it looks like in code

pythonThe core declares which gateway it needs; the concrete implementation is plugged in from outside
# core/orders.py — knows only about the domaindef place_order(cart, payment_gateway):    if not cart.items:        raise EmptyCart    return payment_gateway.charge(cart.total()) # adapters/payments.py — the detail depends on the core, not the other way roundclass StripeGateway:    def charge(self, amount):        return stripe.Charge.create(amount=amount.kopecks, currency="usd")

What it provides and when to use it

  • Business logic is tested without a database or framework — tests are fast and independent of the environment.
  • Details change with less pain: replacing the ORM or the web framework means rewriting the outer circle, not the core.
  • Boundaries are visible in the project structure: domain rules are not smeared across the whole codebase.
  • In mobile development it separates the core with the rules from the platform SDK and the UI framework.
  • It pays off where rules are complex and long-lived: banking, insurance, billing. For typical CRUD applications the price is extra layers and data conversions.

The term was popularised by Robert Martin: the 2012 article “The Clean Architecture” and the 2017 book “Clean Architecture”. He brought related approaches under a single rule: Alistair Cockburn’s hexagonal architecture with ports and adapters, and Jeffrey Palermo’s onion architecture. The circles are named differently in the variants, the rule is the same.

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • layered architecture

    In a layered architecture dependencies point downwards through the layers; in clean architecture they point strictly inwards, towards the business rules.

  • hexagonal architecture

    Hexagonal architecture describes the same idea through ports and adapters; clean architecture adds the circles and a strict dependency rule.

In depth in the references

where the topic is covered in detail