A production-ready Clean Architecture template for ASP.NET Core 9 applications with CQRS, Entity Framework Core, PostgreSQL, and comprehensive testing setup.
This template follows Clean Architecture principles with clear separation of concerns:
┌─────────────────────────────────────────────────────────────┐
│ WebAPI │
│ (Controllers, Middleware) │
├─────────────────────────────────────────────────────────────┤
│ Infrastructure │
│ (Data Access, External Services) │
├─────────────────────────────────────────────────────────────┤
│ Application │
│ (Business Logic, Use Cases) │
├─────────────────────────────────────────────────────────────┤
│ Domain │
│ (Entities, Value Objects) │
└─────────────────────────────────────────────────────────────┘
CleanArchitectureTemplate/
├── src/
│ ├── Domain/ # Core business logic and entities
│ │ ├── Common/
│ │ ├── Entities/
│ │ ├── Enums/
│ │ ├── ValueObjects/
│ │ └── Events/
│ ├── Application/ # Business use cases and application logic
│ │ ├── Common/
│ │ │ ├── Behaviours/
│ │ │ ├── Interfaces/
│ │ │ ├── Models/
│ │ │ └── Mappings/
│ │ └── Features/
│ │ └── SampleEntity/
│ │ ├── Commands/
│ │ └── Queries/
│ ├── Infrastructure/ # Data access and external services
│ │ ├── Data/
│ │ │ ├── Configurations/
│ │ │ └── Interceptors/
│ │ ├── Services/
│ │ └── Identity/
│ └── WebAPI/ # API controllers and configuration
│ ├── Controllers/
│ ├── Middleware/
│ ├── Extensions/
│ └── Filters/
├── tests/
│ ├── UnitTests/ # Isolated unit tests
│ └── IntegrationTests/ # End-to-end integration tests
└── docs/ # Documentation
- .NET 9.0 SDK
- PostgreSQL (local or Docker)
- Your favorite IDE (Visual Studio, VS Code, Rider)
-
Clone and Rename
git clone <github link> cd your-new-project
-
Update Project Names (Optional)
- Replace "CleanArchitectureTemplate" in solution file
- Update namespaces throughout the projects
- Rename projects if desired
-
Configure Database
- Check .env.example to configure database connection and Jwt settings
-
Run the Application
dotnet restore dotnet build dotnet run --project src/WebAPI
- .NET 9.0 - Latest .NET framework
- ASP.NET Core 9.0 - Web API framework
- Entity Framework Core 9.0 - ORM for data access
- PostgreSQL - Primary database
- MediatR - CQRS and Mediator pattern implementation
- AutoMapper - Object-to-object mapping
- FluentValidation - Request validation
- ASP.NET Identity - User management
- JWT Bearer - Token-based authentication
- Serilog - Structured logging
- Swagger/OpenAPI - API documentation
- xUnit - Testing framework
- Moq - Mocking framework
- FluentAssertions - Readable assertions
- TestContainers - Integration testing with real databases
-
Domain Layer (Core)
- Contains enterprise business rules
- Entities, Value Objects, Domain Events
- No dependencies on other layers
-
Application Layer
- Contains application business rules
- Use cases, Commands, Queries (CQRS)
- Depends only on Domain layer
-
Infrastructure Layer
- Contains framework and external concerns
- Database access, external APIs, file systems
- Implements interfaces defined in Application layer
-
WebAPI Layer
- Contains controllers and API configuration
- Depends on Application and Infrastructure layers
- Entry point for HTTP requests
- CQRS (Command Query Responsibility Segregation)
- Repository Pattern with Unit of Work
- Dependency Injection
- Domain Events
- Specification Pattern (optional)
# Run all tests
dotnet test
# Run unit tests only
dotnet test tests/UnitTests
# Run integration tests only
dotnet test tests/IntegrationTests
# Run with coverage
dotnet test --collect:"XPlat Code Coverage"- Unit Tests: Fast, isolated tests for business logic
- Integration Tests: Test complete request/response cycles
- TestContainers: Real database instances for integration testing
- Create Entity (Domain layer)
- Create Commands/Queries (Application layer)
- Create Handlers (Application layer)
- Create Controllers (WebAPI layer)
- Add Tests (Unit and Integration)
// 1. Domain/Entities/Product.cs
public class Product : BaseEntity
{
public string Name { get; set; }
public decimal Price { get; set; }
}
// 2. Application/Features/Products/Commands/CreateProduct.cs
public record CreateProductCommand(string Name, decimal Price) : IRequest<Guid>;
public class CreateProductHandler : IRequestHandler<CreateProductCommand, Guid>
{
// Implementation
}
// 3. WebAPI/Controllers/ProductsController.cs
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
// Implementation
}# Add Dockerfile for containerization
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore
RUN dotnet build -c Release -o /app/build
FROM build AS publish
RUN dotnet publish -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebAPI.dll"]- Development:
appsettings.Development.json - Production:
appsettings.Production.json - Environment variables for sensitive data
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- 📧 Create an issue for bugs or feature requests
- 💬 Discussions for questions and ideas
- ⭐ Star this repository if you find it helpful!
Happy Coding! 🎉