Clean Architecture Setup
Project Structure
MyProject/
├── src/
│ ├── Domain/
│ │ ├── Entities/
│ │ ├── ValueObjects/
│ │ └── Interfaces/
│ ├── Application/
│ │ ├── Features/
│ │ │ ├── Products/
│ │ │ │ ├── Commands/
│ │ │ │ └── Queries/
│ │ ├── Common/
│ │ └── Interfaces/
│ ├── Infrastructure/
│ │ ├── Data/
│ │ ├── Services/
│ │ └── Repositories/
│ └── WebAPI/
│ ├── Controllers/
│ ├── Middleware/
│ └── Program.cs
└── tests/
├── UnitTests/
└── IntegrationTests/
Layer Responsibilities
Domain Layer
- Entities and value objects
- Domain logic
- Domain events
- Interfaces (no implementations)
Application Layer
- Use cases (commands/queries)
- DTOs
- MediatR handlers
- Validation
Infrastructure Layer
- EF Core DbContext
- External services
- File storage
- Caching
WebAPI Layer
- Controllers/Endpoints
- Middleware
- Configuration
- Dependency injection setup
Implementation
// Domain Entity
public class Product
{
public int Id { get; private set; }
public string Name { get; private set; }
public Money Price { get; private set; }
public void UpdatePrice(Money newPrice)
{
// Domain logic
Price = newPrice;
}
}
// Application Command
public record CreateProductCommand(string Name, decimal Price) : IRequest<int>;
public class CreateProductCommandHandler : IRequestHandler<CreateProductCommand, int>
{
public async Task<int> Handle(CreateProductCommand request, CancellationToken ct)
{
// Business logic
}
}