Most "Clean Architecture" Repos Enforce the Dependency Rule by Convention. Mine Fails the Build Instead.
Lesson 1 of a free, opinionated .NET architecture course - with a runnable .NET 10 sample and a real EF Core gotcha. Originally published on adrianbailador.github.io - this is a cross-post with a canonical link back to the original.
I'm putting together a free course on software architecture: the styles, patterns, and practices that actually show up in production .NET codebases, not the ones that only exist in conference slides. Ten lessons, one per week, each paired with a companion repository you can clone and run - code, not just diagrams.
The goal isn't to crown a "best" architecture. It's that you can look at a real project and know which of these actually fits it, which ones are being cargo-culted, and how to combine them without creating an indirection factory nobody can navigate six months later.
This is Lesson 1.
Clean Architecture: Explained and Reviewed
Clean Architecture's real contribution isn't the concentric-circles diagram - it's the Dependency Rule: source code dependencies only point inward, toward the domain, never outward toward frameworks or infrastructure. Everything else, the specific layer names, whether you call it "Application" or "UseCases", is naming, not substance.
It earns its complexity in systems with real domain logic worth protecting from framework churn: long-lived codebases, teams big enough that "where does this code go" needs an answer everyone agrees on.
It's overkill for CRUD-shaped services with no meaningful business rules, where you'll spend more time mapping between layers than the mapping ever saves you.
Repository: clean-architecture-dotnet10 - a .NET 10 order-placement API: Domain → Application → Infrastructure (EF Core) → Api (minimal APIs), 26 tests, CI on every push.
The rule, enforced by a test, not a diagram
Most "Clean Architecture" repos enforce the Dependency Rule by convention and just hope nobody breaks it in a rushed PR. This one enforces it with a reflection test that fails the build the moment it's violated:
[Fact]
public void Domain_does_not_reference_any_Entity_Framework_package()
{
Assert.DoesNotContain(
ReferencedAssemblyNames(DomainAssembly),
name => name.StartsWith("Microsoft.EntityFrameworkCore"));
}
If someone adds using Microsoft.EntityFrameworkCore; to a class in CleanArchitecture.Domain six months from now because it was the path of least resistance, this test fails in CI - not in a code review nobody had time to do properly.
Where the one business rule lives
This sample is built around one thing: a 10% loyalty discount on orders of $100 or more, and where it lives - exactly one place.
public Money Total()
{
var subtotal = Subtotal();
return subtotal.Amount >= LoyaltyDiscountThreshold
? subtotal.ApplyDiscount(LoyaltyDiscountPercentage)
: subtotal;
}
PlaceOrderInteractor in the Application layer calls order.Total(). It has no idea a discount exists. Neither does the EF Core repository, or the minimal API endpoint.
That's the Dependency Rule paying off in a way a diagram never shows you: for this rule specifically, the only file that has to change is Order.cs - not every future business rule stays this contained, but the ones that genuinely belong to the domain usually do.
What the repo doesn't hide: the EF Core gotcha
An early draft of this sample threw No suitable constructor was found for the type 'OrderLine' the first time it actually ran, not when it compiled. OrderLine's only public constructor takes a Money parameter, and Money is mapped as an owned reference, not a scalar column - so EF Core can't bind it through a constructor the way it binds a string or an int.
The fix is a private, EF-only parameterless constructor, so EF falls back to setting properties through their backing fields instead:
private OrderLine()
{
ProductName = string.Empty;
UnitPrice = Money.Zero;
}
That's the kind of friction Clean Architecture is supposed to contain to one layer, and here it does: the fix lives entirely in CleanArchitecture.Domain's OrderLine.cs and the mapping configuration in CleanArchitecture.Infrastructure. PlaceOrderInteractor, the tests that exercise it, and the API endpoint never had to change.
What's coming next
Lesson 1 is live now. The next nine ship weekly:
- Hexagonal Architecture
- Onion Architecture
- MVC
- MVVM
- Vertical Slice Architecture
- CQRS
- Event-Driven Architecture
- Domain-Driven Design
- Test-Driven Development
Each with its own runnable repo.
The full syllabus, the live lessons, and a free email alert for each new one are here: Software Architecture Fundamentals - the full course.
Adrian Bailador Panero is a Software Engineer writing about .NET, C#, and software architecture. Originally published at adrianbailador.github.io. He also builds The Coffee Timer, a Pomodoro-style focus timer with task tracking and a productivity calendar - now on Android.
Comments
No comments yet. Start the discussion.