Architectureonion architecturedependency rulecomposition root

Clean architecture

Definition

Clean architecture organises code into concentric rings (entities, use cases, interface adapters, frameworks and drivers) and follows the dependency rule: source code may depend only on rings closer to the centre.

In practice

The core declares the interfaces it needs, such as IOrderRepository; adapters in outer rings implement them, and a composition root at startup wires the implementations in. Control still flows outward at runtime, but dependencies point inward, so the database or web framework can be swapped and the core tested without infrastructure.

The price is extra interfaces, mapping between layers and more files per feature. For CRUD services with little business logic it is often overkill, and wrapping every library call in an abstraction "just in case" adds indirection without benefit. It is closely related to hexagonal and onion architecture.

Example

The PlaceOrder use case depends on IOrderRepository and IPaymentGateway; Program.cs registers SqlOrderRepository and StripePaymentGateway, while tests register in-memory fakes.