What it is
Clean architecture, described by Robert C. Martin in 2012, organises an application as concentric rings around its business rules:
- Entities: enterprise-wide business objects and rules (
Order,Money). - Use cases: application-specific rules that orchestrate entities (
PlaceOrder,CancelOrder), plus the interfaces they need from the outside. - Interface adapters: code that converts between the use cases and the outside world (endpoints and controllers, presenters, repository and gateway implementations).
- Frameworks and drivers: ASP.NET Core, EF Core, the database, message brokers, third-party APIs.
One rule governs it, the dependency rule: source code dependencies point only inward. Nothing in an inner ring may name anything in an outer ring. Onion architecture (Jeffrey Palermo, 2008) and hexagonal architecture (Alistair Cockburn, 2005) express the same idea with different pictures.
Why it exists
In a traditional layered application the domain depends on the data layer, so the business rules know about tables, ORMs and connection handling. Database changes ripple into the rules, and the rules cannot be tested without a database. Clean architecture turns that dependency around so the most valuable and stable code, the business rules, depends on nothing volatile. Frameworks, UIs and databases become replaceable details.
How it works
The core owns its interfaces. The use case declares what it needs:
// Shop.Application
public interface IOrderRepository { Task Add(Order order, CancellationToken ct); }
public interface IPaymentGateway { Task<PaymentResult> Charge(Money amount, CancellationToken ct); }
public sealed class PlaceOrder(IOrderRepository orders, IPaymentGateway payments)
{
public async Task<Guid> Handle(PlaceOrderCommand cmd, CancellationToken ct)
{
var order = Order.Place(cmd.CustomerId, cmd.Lines); // entity rules
var result = await payments.Charge(order.Total, ct);
order.MarkPaid(result.Reference);
await orders.Add(order, ct);
return order.Id;
}
}
Adapters implement them. Shop.Infrastructure references Shop.Application and provides SqlOrderRepository : IOrderRepository using EF Core. The Application project has no reference to Infrastructure or to EF Core.
The composition root wires it. The host (Program.cs) is the outermost code and references everything: builder.Services.AddScoped<IOrderRepository, SqlOrderRepository>();. The core receives implementations through constructor injection.
Control flow versus dependency. At runtime PlaceOrder calls the repository, so control flows outward. The source dependency points inward because the call goes through an interface the core declared. This is the dependency inversion principle applied at the architecture level.
Crossing the boundary with data. Data crossing a ring boundary should be in a form the inner ring defines: commands and results for use cases, not HttpRequest objects or EF entities.
When to use it
- Rich, long-lived business rules that must be tested thoroughly.
- Systems where infrastructure is expected to change or has several implementations (multiple storage back ends, payment providers per market).
- Teams that want the domain model free of persistence and transport concerns, often together with domain-driven design.
When not to
- CRUD applications and small services with little logic: the extra projects, interfaces and mapping outweigh the benefit.
- Prototypes and short-lived tools.
- When the team would add interfaces mechanically without understanding why; the ceremony then remains while the benefit does not.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Interfaces owned by the core | Replaceable infrastructure, fast unit tests | One more type per dependency, indirection when reading code |
| Separate domain and persistence models | Domain free of ORM concerns | Mapping code and its bugs |
| Domain mapped directly by EF Core (common compromise) | Less mapping | Some persistence concerns leak into entities |
| Projects per ring | Compiler-enforced dependency direction | Longer builds, more navigation |
Alternatives
- Layered architecture: simpler, but the domain depends on data access.
- Hexagonal architecture: the same inversion, described as a core with ports and adapters on a driving and a driven side, with no prescribed inner rings.
- Vertical slice architecture: organise by feature and keep abstraction to what each slice needs; often combined with a clean core for the domain.
Failure modes
- Interface for everything: one-implementation interfaces for classes that never vary, adding indirection without a seam that is ever used.
- Leaky core:
IQueryable, EF attributes orHttpContextcreep into use cases, quietly breaking the rule. - Anemic entities: all rules live in use cases; the entity ring holds only data.
- Mapping bugs: a field added to one model but not mapped to another silently disappears.
- Folder-only architecture: rings exist as folders in one project, so nothing stops a wrong reference. Use separate projects or architecture tests.
In production
Common .NET layouts are Domain, Application, Infrastructure and Web (or Api) projects. Enforce the direction with project references and an architecture test (NetArchTest, ArchUnitNET) in CI. Keep fast unit tests on use cases with fakes, and run integration tests for the adapters against a real database (for example with Testcontainers), because fakes cannot prove that SQL and mappings are right. Consider letting EF Core map domain entities directly when the mapping cost is not worth it, as a conscious decision.
Common misconceptions
- "Dependencies inward means calls inward." Calls go outward through interfaces; only source references point in.
- "Clean architecture means four projects." The rule matters, not the project count. A single project with enforced rules can follow it; four projects with a leaky core do not.
- "You can swap the database for free." The code change is contained. Data migration, transactions and query behaviour still need work.
- "Use cases are just services." A use case represents one application action with its own input and output, rather than a grab-bag of methods.
Interview angle
State the dependency rule and draw the rings, then show the inversion: interface in the core, implementation outside, wiring in the composition root. Explain that control flow goes outward while dependencies point inward. Strong answers name the costs (types, mapping, indirection), say when it is overkill, and relate it to hexagonal and onion architecture as variations of the same idea.
Model assumptions
This lesson is a scripted illustration, not a simulation: the queueing engine models running components exchanging requests, while clean architecture is about how source code depends on other source code. Arrows in the scene are source-code dependencies; packets are hand-placed to show either runtime calls or, as pulses, which way a dependency or a change travels. Packets are slowed to 30–60 ms per hop so they can be followed; real in-process calls take microseconds. Project and class names are examples of a typical .NET layout, not the only correct one.