Clean Architecture (Robert C. Martin) separates concerns into concentric rings, with the Domain at the center. Dependencies point inward. We implement this in .NET 5.
The Layers
flowchart TB
subgraph Core ["Core (No Dependencies)"]
Domain[Domain Entities]
App[Application Use Cases]
end
subgraph Outer ["Infrastructure"]
Infra[EF Core / SQL]
API[Web API Controllers]
end
App --> Domain
Infra --> App
API --> App
style Core fill:#FFF3E0,stroke:#E65100
The Dependency Rule
The Application layer defines interfaces (e.g., `IOrderRepository`), but the Infrastructure layer implements them. This “Inversion of Control” keeps the core independent of the database.
// Core/Interfaces/IOrderRepo.cs
public interface IOrderRepository
{
Task SaveAsync(Order order);
}
// Infrastructure/Persistence/OrderRepo.cs
public class OrderRepository : IOrderRepository
{
private readonly DbContext _context;
public async Task SaveAsync(Order order) => await _context.AddAsync(order);
}
Key Takeaways
- Domain entities should have no external dependencies (no attribute tags if possible).
- Use CQRS (MediatR) in the Application layer to handle use cases.
- Infrastructure maps Domain entities to Database tables.
Discover more from C4: Container, Code, Cloud & Context
Subscribe to get the latest posts sent to your email.