Clean Architecture in .NET: Practical, Core Over Framework
In the world of .NET development, there's a lot of talk about architecture, layers, and clean code, but the essence is simpler than it often appears. Good architecture is one that lasts. One that survives new features, technology changes, new team members, and application growth. The approach that has proven itself best is one that clearly separates domain logic (the reason the system exists) from everything that surrounds it.
Clean Architecture starts from one clear principle: business rules must be isolated and must not depend on infrastructure. Technology changes. The domain should remain stable.
What Such a Project Looks Like
First, the application structure is divided by responsibilities:
/src
Project.Domain // rules, entities, value types
Project.Application // use-case logic, commands, orchestration
Project.Infrastructure // EF Core, databases, API integrations
Project.Api // HTTP endpoints, presentation layer- Domain sits at the center.
- Application layer around it.
- API and Infrastructure at the periphery.
Dependencies flow exclusively inward. No inner layer knows about the outer ones.
The domain layer is clean, without Entity Framework, without databases, without network, without web server.
Domain as the Heart of the System
In practice, this means an entity is not just a data model. It's a carrier of rules. It knows what can and cannot be done to it. If an entity has only properties and nothing more, then it's not a domain. It's a mapped database record.
An example of a domain model for an order might look like this:
public class Order
{
private readonly List<OrderLine> _lines = new();
public Guid Id { get; private set; }
public IReadOnlyCollection<OrderLine> Lines => _lines.AsReadOnly();
public decimal Total => _lines.Sum(l => l.TotalPrice);
public void AddLine(Guid productId, int quantity, decimal price)
{
if (quantity <= 0)
throw new InvalidOperationException("Quantity must be greater than zero.");
_lines.Add(new OrderLine(productId, quantity, price));
}
public void Confirm()
{
if (!_lines.Any())
throw new InvalidOperationException("Incomplete order cannot be confirmed.");
// other rules...
}
}public class OrderLine
{
public Guid ProductId { get; }
public int Quantity { get; private set; }
public decimal UnitPrice { get; }
public decimal TotalPrice => UnitPrice * Quantity;
public OrderLine(Guid productId, int quantity, decimal price)
{
if (quantity <= 0)
throw new InvalidOperationException("Invalid quantity.");
ProductId = productId;
Quantity = quantity;
UnitPrice = price;
}
}Here the entity is not passive. It defends the system's rules. It knows what a valid state means.
When logic stays with data, the system becomes easier to understand, test, and extend. Rules don't spill across services, controllers, and handlers. They're centralized where they belong.
Request Flow in This Architecture
When the application receives an HTTP request, it doesn't go directly to the database. The API layer forwards it to a Use-Case handler, which reads the entity from the repository, calls the appropriate domain method, and finally writes the changes back to the data store.
A simple handler might look like this:
public async Task ConfirmOrder(Guid id)
{
var order = await _orders.GetById(id);
order.Confirm(); // domain does the work
await _orders.Save(order); // infrastructure just saves the result
}- Handler orchestrates.
- Domain decides.
And when you want to test system rules, you don't need to spin up a database, API, network, or DI container. It's enough to instantiate the entity and call methods.
var order = new Order();
order.AddLine(productId, 2, 10m);
order.Confirm();
Assert.Equal(20m, order.Total);That's a domain test, not a framework test. That's why Clean Architecture pays off in the long run.
Conclusion
Clean Architecture in .NET is not a magical pattern. It's not a diagram, not a trend, and not a buzzword. It's a way for a system to outlive the current technology it's built on.
When the domain is protected and isolated, business rules remain understandable, testable, and consistent regardless of whether the UI, API, database, or the team working on the project changes.
- Technology can be replaced.
- Architecture can be refactored.
- But a well-defined domain remains.