Topic 051 / Advanced
Clean architecture
Chapter 4.4 · Dependencies and Application Structure
What and why
Plain layered architecture (→ 50) is enough until the domain is rich and long-lived — then "domain depends on nothing above it" is not a strong enough guarantee, because a change to a database schema or a UI framework can still ripple into business rules if the domain layer merely sits below infrastructure rather than being actively protected from it. Clean architecture puts the domain at the center of concentric rings, with every dependency pointing inward: outer rings (frameworks, databases, UI) depend on inner rings (use cases, entities) through interfaces the inner rings define — the domain never knows the outer rings exist, even by name. Builds on: → 50.
Diagram
┌───────────────────────────────┐
│ Frameworks & Drivers │ (DB, web framework, UI)
│ ┌──────────────────────────┐ │
│ │ Interface Adapters │ │ (controllers, repository implementations)
│ │ ┌─────────────────────┐ │ │
│ │ │ Use Cases │ │ │ (application logic)
│ │ │ ┌────────────────┐ │ │ │
│ │ │ │ Entities │ │ │ │ (domain rules — innermost)
│ │ │ └────────────────┘ │ │ │
│ │ └─────────────────────┘ │ │
│ └──────────────────────────┘ │
└────────────────────────────────┘
Arrows always point inward; the innermost ring defines the interfaces
the outer rings implement.
How it works
- Model Entities and Use Cases with zero imports from any framework, database driver, or UI library.
- Define the interfaces the inner rings need (a repository contract, a clock contract) inside the inner rings, not in the layer that implements them.
- Implement those interfaces in the outer rings, wiring concrete classes at the application's entry point — the one composition root that is allowed to know every concrete type.
- Changing the database or the web framework means rewriting an outer-ring adapter; the use cases and entities do not change.
- Verify the rule mechanically: a build or import check that fails if anything under Entities/Use Cases imports from Interface Adapters or Frameworks.
Practical example
(illustrative). In the task manager, CompleteTaskUseCase
depends on a TaskRepository interface and a Clock interface, both defined next to the
use case; PostgresTaskRepository and SystemClock live in the outer ring and are wired
in only at startup — swapping the database touches only the outer-ring adapter class.
When to use it
- Use when: the domain is rich, long-lived, and expected to outlive a specific framework or database choice — the investment pays back over years, not weeks.
- Avoid when: the application is CRUD-shaped with thin business rules — the composition root and four rings of interfaces cost more to navigate than the plain layering (→ 50) they replace.
- Failure scenarios: "clean architecture" where the use case still imports an ORM entity base class for convenience — the rings exist in folder names only, and the promised framework-independence is false the first time anyone checks it.
- Simpler alternative: layered architecture (→ 50) for the common case; reach for clean architecture only once a second UI, a second storage technology, or genuine multi-year domain longevity is a real, not hypothetical, requirement.
Practice
(25–30 min; no lab services — any language). Take the layered task-manager example from → 50 and tighten it: define the repository and clock interfaces inside the use-case module; move every concrete implementation to an outer adapters module wired only at the entry point. Done when the Postgres adapter can be replaced by an in-memory one without touching the use-case file.
Key takeaway
Clean architecture is layering (→ 50) with the dependency rule enforced strictly inward and the interfaces owned by the center. Done when you can: identify the one file in your system that is allowed to know every concrete class. Used next in → 52 (hexagonal architecture is the same inward-dependency rule under different names), → 53 (modular monolith can apply this rule inside each module).
References
None — this topic makes no external claims.
Source directory