1 min read
Clean Architecture with .NET
- .NET
- Architecture
- SOLID
Why Clean Architecture?
Clean architecture aims to keep business rules independent from frameworks and infrastructure details. As a result, the core of the application is not affected when the database, UI, or external services change.
Layers
- Domain: Entities and business rules. No external dependencies.
- Application: Use-cases, CQRS handlers and interfaces.
- Infrastructure: Details like EF Core, messaging, file system.
- API/Presentation: Controllers and entry points.
The dependency arrow always points inward, toward the Domain.
Example
public interface IOrderRepository
{
Task<Order?> GetByIdAsync(Guid id);
Task AddAsync(Order order);
}
The Application layer depends on this interface; the concrete EF Core
implementation lives in the Infrastructure layer. This makes it easy to mock
the repository in unit tests.
Conclusion
While clean architecture may feel like over-abstraction at first, it significantly reduces maintenance cost as projects grow.