Clean Architecture keeps your business logic independent of frameworks and databases. Here’s how I implement it in .NET projects.
The Layers
- Domain: Entities, value objects, domain events
- Application: Use cases, interfaces, DTOs
- Infrastructure: Database, external services
- WebAPI: Controllers, middleware
Project Structure
src/
├── MyApp.Domain/
├── MyApp.Application/
├── MyApp.Infrastructure/
└── MyApp.WebAPI/Dependency Rule
Dependencies point inward. Domain has no dependencies. Application depends on Domain. Infrastructure depends on Application.
Example
// Application layer defines interface
public interface IOrderRepository
{
Task<Order> GetByIdAsync(int id);
}
// Infrastructure implements it
public class OrderRepository : IOrderRepository
{
private readonly AppDbContext _context;
public async Task<Order> GetByIdAsync(int id)
=> await _context.Orders.FindAsync(id);
}References
Discover more from C4: Container, Code, Cloud & Context
Subscribe to get the latest posts sent to your email.