الانتقال إلى المحتوى
.NETClean Architecture· 6 دقائق للقراءة

Clean Architecture Is a Dependency Rule, Not Four Projects

The Domain, Application, Infrastructure and Api projects are one way to enforce the rule. What makes a design clean is the direction its dependencies point.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • .NET 10
محتويات المقال

A team adds a message consumer that has to place orders too: the same use case, triggered by a queue instead of a POST. The solution looks like the textbook, with Domain, Application, Infrastructure and Api projects and references pointing the right way. Yet PlaceOrderHandler can't be called from the worker. It reads the user from IHttpContextAccessor and returns IResult, and the Application project compiles only because someone added a framework reference to ASP.NET Core months ago. Four projects, and the business logic still depends on the web.

The common misconception

The belief: Clean Architecture is a solution layout. Create Domain, Application, Infrastructure and Api projects, point the project references inward, and the architecture is clean.

It's easy to see where this comes from. Popular templates ship exactly that shape, and Microsoft's architecture guide shows its Application Core, Infrastructure and UI layers as projects. The famous diagram has four circles, so four projects is a natural mapping. Projects are visible in the solution explorer, easy to review, and the compiler enforces their references, which feels like a guarantee.

The half-truth: projects are a good way to enforce the rule. They aren't the rule.

What actually happens

One rule: dependencies point toward policy

Martin's 2012 post draws concentric circles: entities with enterprise-wide business rules at the center, use cases around them, interface adapters next, and frameworks and drivers outside, where the web and the database live as details. The holds it together: source code dependencies point only inward. Inner code doesn't mention anything declared in an outer circle, whether that's a class, a function, a variable or a data format a framework defines.

Calls go out, dependencies still point in

A use case still needs the current user, the database and the email server. It gets them through : the inner layer declares an interface for what it needs, and an outer layer implements it. At run time the call flows outward; at compile time the outer code depends on the inner interface. Microsoft's guide describes the same arrangement: abstractions defined in the Application Core, implemented by types in Infrastructure, and connected with in the app's , usually Program.cs. That's why the guide lets the web project reference Infrastructure, but only for that wiring.

The circles are schematic

Martin says so directly: the circles are schematic, you may need more than four, and the one constant is that the Dependency Rule always applies. Microsoft's guide adds that layers are a logical separation. A single-project app can separate concerns with folders. Splitting into projects adds something specific: the compiler checks that each project uses only the projects it references.

Projects check references, not the rule

That check is narrower than it looks. Nothing stops someone adding a framework reference to ASP.NET Core or a package reference to EF Core in the Application project, and from then on the compiler lets inner code name HttpContext or DbSet<T> without complaint. Martin's advice to pass simple data structures across boundaries, rather than entities or database rows, is also beyond what a project reference can check.

The fix: let the inner circle own what it needs

C#
// Application project, which now has a framework reference to ASP.NET Corepublic sealed class PlaceOrderHandler(    IHttpContextAccessor http, IOrderStore orders){    public async Task<IResult> Handle(PlaceOrder cmd, CancellationToken ct)    {        // Only an HTTP request can supply this, so a worker can't call it        var userId = http.HttpContext!.User.FindFirst("sub")!.Value;        if (cmd.Lines.Count == 0) return Results.BadRequest("Empty order");        var id = await orders.AddAsync(userId, cmd.Lines, ct);        return Results.Created($"/orders/{id}", id);    }}

The handler now asks for an ICurrentUser that it declares itself, and it returns a result that says nothing about HTTP. The Api project implements the interface, Program.cs registers HttpCurrentUser for ICurrentUser, and the endpoint turns Created into a 201 and Rejected into a 400. The worker registers its own ICurrentUser that reads the user from the message, and calls the same handler. None of this needed a fifth project, or even a second one: it's a change of direction, not of layout.

MythReality
Clean Architecture means Domain, Application, Infrastructure and Api projectsIt means the Dependency Rule; Martin calls the circles schematic
Four projects guarantee clean dependenciesProjects check project references only; a package or framework reference can still pull the web or EF Core inward
A single project can't be cleanIt can follow the rule with folders; you lose the compiler's check and need tests or reviews instead
Inner layers never call outer codeThey call it at run time, through interfaces they own
Entities travel across every layerMartin: pass simple data structures across boundaries, not entities or rows

How to apply it in your project

  1. List what the inner code can seeRun dotnet reference list and dotnet package list on Domain and Application. In a single project, search the inner folders for using Microsoft.AspNetCore and using Microsoft.EntityFrameworkCore.
  2. Give every outward need a portCurrent user, clock, email, payments, file storage: declare an interface in the inner layer, implement it in the outer layer, and register it in Program.cs.
  3. Translate at the edgeEndpoints map requests to commands and results to status codes. Use cases take plain input and return plain results.
  4. Enforce the directionUse separate projects for a compile-time check, or an architecture test in CI for a single project, and review every new reference added to inner code.

Key takeaways

  • Clean Architecture is one rule: source code dependencies point inward, toward business policy.
  • The circles, and the four projects, are schematic; the Dependency Rule is what always applies.
  • Projects make the compiler check project references, nothing more; a package or framework reference can still break the rule.
  • Calls flow outward at run time through interfaces the inner layer owns, wired up in the composition root.
  • If a use case can't run without the web or the database, the arrows point the wrong way.

📚 المصادر

  1. The Clean Architecture — Robert C. Martin, The Clean Code Blog (2012)
  2. Common web application architectures — .NET Architecture Guides, Microsoft Learn
  3. Architectural principles — .NET Architecture Guides, Microsoft Learn

التعليقات (0)