Why the Dependency Rule Is the Real Soul of Clean Architecture
The article explains that Clean Architecture’s true essence lies in the immutable Dependency Rule—dependencies must always point inward—while the four‑layer diagram is merely a flexible skeleton, and it clarifies common misconceptions, ties to SOLID, and shows how to apply the rule across Hexagonal and Onion variants.
Dependency Rule
The core constraint of Clean Architecture is that source‑code dependencies may only point inward, from outer layers toward inner layers. Inner layers must never reference, import, or know anything about outer layers.
Four‑layer decomposition (default)
Entities
Responsibility: encapsulate enterprise‑wide business rules (e.g., "order total = price × quantity + shipping").
Dependency direction: does not depend on any other layer.
Typical components: business‑rule objects, domain models.
Use Cases
Responsibility: encapsulate application‑level business rules and orchestrate entities (e.g., a "place order" use case calls entity rules, coordinates steps).
Dependency direction: depends only on Entities.
Typical components: Interactor (use‑case class).
Interface Adapters
Responsibility: translate inner‑layer data structures to formats required by external systems (HTTP requests, database queries, UI rendering).
Dependency direction: depends on Use Cases and Entities.
Typical components: Controller, Presenter, Gateway.
Frameworks & Drivers
Responsibility: concrete technical details such as database drivers, web frameworks, UI libraries.
Dependency direction: depends on Interface Adapters.
Typical components: MySQL driver, Spring/Ktor web adapters, React/Thymeleaf UI libraries.
Crossing the boundary with Dependency Inversion
When an outer layer needs to invoke inner‑layer behavior (e.g., persisting an order), the inner layer defines an output‑port interface:
interface OrderRepository {
fun save(order: Order)
}The outer Frameworks & Drivers layer provides an implementation:
class MySQLOrderRepository : OrderRepository {
override fun save(order: Order) { /* JDBC logic */ }
}Both the use‑case (caller) and the concrete repository depend on the abstraction OrderRepository, reversing the dependency direction so that the outer layer points inward.
Testability as a design goal
Because inner layers have no dependencies on outer details, they can be exercised with mocks or fakes without starting a database or web server. If a test requires the real framework, the dependency rule has been violated.
Relation to Hexagonal and Onion architectures
Clean Architecture shares the same invariant with Hexagonal (Alistair Cockburn, 2005) and Onion (Jeffrey Palermo, 2008) architectures: dependencies always point toward the core. The naming differs—Clean emphasizes the rule, Hexagonal stresses ports and adapters, Onion visualizes concentric circles—but the underlying principle is identical.
SOLID as the genetic source
SRP → layer boundaries : each layer has a single reason to change (Entities change when business rules evolve, Use Cases when workflows change, Interface Adapters when external formats change, Frameworks when technology changes).
DIP → dependency direction : high‑level modules (inner layers) do not depend on low‑level modules (outer layers); both depend on abstractions, which is the concrete manifestation of the Dependency Rule.
OCP → extensibility without modification : new databases or new use cases are added by creating new implementations in the outer layer or new interactors in the use‑case layer, leaving inner code untouched.
Three common misreadings clarified
"Clean Architecture is just a four‑layer diagram" – the number of layers is flexible; the immutable rule is the real constraint.
"All business logic belongs in the Use‑Case layer" – the Entity layer holds enterprise‑wide rules, while the Use‑Case layer handles application‑specific flows.
"Clean Architecture only applies to back‑end systems" – the Dependency Rule is language‑agnostic and equally applicable to front‑end, mobile, or any codebase.
Adhering to the Dependency Rule protects the business core from being hijacked by frameworks, databases, or any external detail, yielding a stable and testable architecture.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ZhiKe AI
We dissect AI-era technologies, tools, and trends with a hardcore perspective. Focused on large models, agents, MCP, function calling, and hands‑on AI development. No fluff, no hype—only actionable insights, source code, and practical ideas. Get a daily dose of intelligence to simplify tech and make efficiency tangible.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
