
Clean Architecture Is a Dependency Rule, Not Four Projects
- كاتب موثّق
- .NET 10
محتويات المقال
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
What actually happens
One rule: dependencies point toward policy
Calls go out, dependencies still point in
Program.cs. That's why the guide lets the web project reference Infrastructure, but only for that wiring.
The circles are schematic
Projects check references, not the rule
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
// 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); }}// Application: the use case and the port it owns, with no web typespublic interface ICurrentUser { string Id { get; } }public sealed class PlaceOrderHandler(ICurrentUser user, IOrderStore orders){ public async Task<PlaceOrderResult> Handle( PlaceOrder cmd, CancellationToken ct) { if (cmd.Lines.Count == 0) return new PlaceOrderResult.Rejected("Empty order"); var id = await orders.AddAsync(user.Id, cmd.Lines, ct); return new PlaceOrderResult.Created(id); }}// Api: the outer circle implements the inner interfacepublic sealed class HttpCurrentUser(IHttpContextAccessor http) : ICurrentUser{ public string Id => http.HttpContext?.User.FindFirst("sub")?.Value ?? throw new InvalidOperationException("No signed-in user");}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.
How to apply it in your project
List what the inner code can see Run dotnet reference listanddotnet package liston Domain and Application. In a single project, search the inner folders forusing Microsoft.AspNetCoreandusing Microsoft.EntityFrameworkCore.Give every outward need a port Current 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.Translate at the edge Endpoints map requests to commands and results to status codes. Use cases take plain input and return plain results. Enforce the direction Use 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.
التعليقات (0)