A modern, production-ready .NET 9 Web API template built with Clean Architecture principles, featuring JWT authentication, Entity Framework Core auditing, and comprehensive soft delete functionality. The backend is .NET WebAPI and the frontend is Angular.
This template follows Clean Architecture patterns with clear separation of concerns across multiple layers:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Presentation Layer β
β βββββββββββββββββββββββ βββββββββββββββββββββββββββ β
β β Web API β β Angular Client β β
β β (Controllers) β β (Frontend) β β
β βββββββββββββββββββββββ βββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Application Layer β
β βββββββββββββββββββββββ βββββββββββββββββββββββββββ β
β β CQRS Commands β β DTOs & Validators β β
β β & Queries β β Interfaces β β
β βββββββββββββββββββββββ βββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Domain Layer β
β βββββββββββββββββββββββ βββββββββββββββββββββββββββ β
β β Entities β β Interfaces β β
β β Domain Logic β β Domain Events β β
β βββββββββββββββββββββββ βββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Infrastructure Layer β
β βββββββββββββββββββββββ βββββββββββββββββββββββββββ β
β β Entity Framework β β Identity Provider β β
β β DbContext β β External Services β β
β βββββββββββββββββββββββ βββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Clean Architecture with clear layer separation
- CQRS Pattern with MediatR
- Entity Framework Core for data access
- Dependency Injection throughout all layers
- API Versioning support
- Comprehensive Unit Testing with xUnit and FluentAssertions
- JWT Bearer Authentication with configurable expiration
- Claims-based Security for fine-grained permissions
- Custom Identity Management with Entity Framework Identity
- Secure Password Policies
- Entity Framework Core 9 with MySQL support
- Database Migrations with automatic seeding
- Soft Delete Implementation with audit trail preservation
- Comprehensive Auditing with creation/modification tracking
- Global Query Filters for soft-deleted entities
- RESTful API Design with OpenAPI/Swagger documentation
- Request/Response Validation with FluentValidation
- Structured Error Handling with custom middleware
- CORS Configuration for cross-origin requests
- HTTP Logging for debugging and monitoring
- Angular 16 client application
- PrimeNG UI Components for rich user interface
- JWT Authentication integration with API
- Responsive Design with modern UI/UX
- Serilog Integration with multiple sinks (Console, File, Database)
- Structured Logging with contextual information
- Health Checks for application monitoring
- Performance Monitoring capabilities
- .NET 9 SDK
- MySQL Server 8.0+
- Visual Studio 2022 or Visual Studio Code
- Node.js 18+ (for Angular client)
- Angular CLI 16.2.7+
-
Clone the repository
git clone https://github.com/andyblem/CleanArchitectureTemplate.git cd CleanArchitectureTemplate -
Configure the database connection
Update
appsettings.jsonandappsettings.Development.json:{ "ConnectionStrings": { "DefaultConnection": "server=localhost;user=root;password=YourPassword;port=3306;database=cleanarchitecturedb" } } -
Configure JWT settings
{ "JWTSettings": { "Key": "YourSecretKeyHere_MustBeAtLeast64CharactersLongForHMACSHA512Algorithm", "Issuer": "CoreIdentity", "Audience": "CoreIdentityUser", "DurationInMinutes": "60" } } -
Run database migrations
dotnet ef database update --project CleanArchitecture.Infrastructure.Persistence --startup-project CleanArchitecture.Presentation.Web.Api
-
Run the Web API
dotnet run --project CleanArchitecture.Presentation.Web.Api
-
Run the Angular Client
cd CleanArchitecture.Presentation.Web.Client npm install ng serve -
Access the Applications
- Web API:
http://localhost:5500 - Swagger UI:
http://localhost:5500/swagger - Angular Client:
http://localhost:4200
- Web API:
- Username:
admin@email.com - Password:
Password12345!
POST /api/accounts/authenticate
Content-Type: application/json
{
"email": "admin@email.com",
"password": "Password12345!"
}Include the token in the Authorization header:
Authorization: Bearer your-jwt-token-hereCleanArchitectureTemplate/
βββ CleanArchitecture.Domain/ # Domain Layer
β βββ Common/ # Shared domain concepts
β βββ Entities/ # Domain entities
β βββ Interfaces/ # Domain interfaces
βββ CleanArchitecture.Application/ # Application Layer
β βββ DTOs/ # Data Transfer Objects
β βββ Features/ # CQRS Commands & Queries
β β βββ CQRS/ # Command/Query handlers
β β βββ Parameters/ # Request parameters
β β βββ Requests/ # MediatR requests
β β βββ Validators/ # FluentValidation validators
β βββ Interfaces/ # Application interfaces
β βββ Wrappers/ # Response wrappers
βββ CleanArchitecture.Infrastructure.Persistence/ # Data Layer
β βββ Contexts/ # EF DbContext
β βββ Extensions/ # EF extensions
β βββ Seeders/ # Database seeders
βββ CleanArchitecture.Infrastructure.IdentityProvider/ # Identity Layer
β βββ DTOs/ # Identity DTOs
β βββ Services/ # Authentication services
βββ CleanArchitecture.Infrastructure.Shared/ # Shared Infrastructure
β βββ Identity/ # Identity models
β βββ Services/ # Shared services
βββ CleanArchitecture.Presentation.Web.Api/ # API Layer
β βββ Controllers/ # API controllers
β βββ Extensions/ # API extensions
β βββ Services/ # API-specific services
βββ CleanArchitecture.Presentation.Web.Client/ # Angular Frontend
β βββ src/ # Angular source code
β β βββ app/ # Application components
β β βββ assets/ # Static assets
β β βββ environments/ # Environment configs
β βββ angular.json # Angular CLI config
β βββ package.json # NPM dependencies
β βββ ... # Angular configuration
βββ Tests/ # Test Projects
βββ CleanArchitecture.UnitTests/ # Unit tests
βββ CleanArchitecture.IntegrationTests/ # Integration tests
βββ CleanArchitecture.FunctionalTests/ # Functional tests
GET /api/v1/Books/GetList # Get paginated books list
GET /api/v1/Books/Get/{id} # Get book by ID
POST /api/v1/Books/Post # Create new book
PUT /api/v1/Books/Put/{id} # Update book
DELETE /api/v1/Books/Delete/{id} # Soft delete bookPOST /api/accounts/authenticate # User authentication
GET /api/accounts/claims # Get user claims
GET /api/accounts/userid # Get current user IDGET /health # Application health statusThe Angular client application is generated with Angular CLI version 16.2.7.
Run ng serve for a dev server. Navigate to http://localhost:4200/. The application will automatically reload if you change any of the source files.
Run ng generate component component-name to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module.
Run ng build to build the project. The build artifacts will be stored in the dist/ directory.
Run ng test to execute the unit tests via Karma.
Run ng e2e to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities.
To get more help on the Angular CLI use ng help or go check out the Angular CLI Overview and Command Reference page.
- PrimeNG Components for rich UI elements
- Authentication Guards for route protection
- HTTP Interceptors for automatic JWT token attachment
- Error Handling with user-friendly messages
- Responsive Layout with modern design
- State Management for application data
The application supports MySQL with Entity Framework Core. Configure your connection string in appsettings.json:
{
"UseInMemoryDatabase": false,
"ConnectionStrings": {
"DefaultConnection": "server=localhost;user=root;password=;port=3306;database=cleanarchitecturedb"
}
}Configure JWT settings for authentication:
{
"JWTSettings": {
"Key": "C1CF4B7DC4C4175B6618DE4F55CA4E9B8F2A7D6C5E4F3B2A1987654321ABCDEF",
"Issuer": "CoreIdentity",
"Audience": "CoreIdentityUser",
"DurationInMinutes": "60"
}
}Serilog is configured to log to console, file, and database:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "MySQL",
"Args": {
"connectionString": "server=localhost;user=root;password=;port=3306;database=cleanarchitecturedb",
"tableName": "ApplicationLogs"
}
}
]
}
}The project uses Entity Framework Core with DbContext pattern for data access:
- ApplicationDbContext serves as the main data access layer
- CQRS handlers interact directly with DbContext
- No Repository Pattern - commands and queries use DbContext directly
- Audit interceptors handle automatic auditing
All entities implement IAuditable interface for comprehensive auditing:
public interface IAuditable
{
DateTime CreatedAt { get; set; }
string CreatedBy { get; set; }
DateTime? ModifiedAt { get; set; }
string? ModifiedBy { get; set; }
bool IsDeleted { get; set; }
DateTime? DeletedAt { get; set; }
string? DeletedBy { get; set; }
}// In your command handlers
context.SoftRemove(entity);
await context.SaveChangesAsync();- Creation tracking:
CreatedAt,CreatedBy - Modification tracking:
ModifiedAt,ModifiedBy - Soft delete tracking:
IsDeleted,DeletedAt,DeletedBy - Global query filters automatically exclude soft-deleted entities
The template includes comprehensive testing setup:
# Unit Tests
dotnet test CleanArchitecture.UnitTests
# Integration Tests
dotnet test CleanArchitecture.IntegrationTests
# Functional Tests
dotnet test CleanArchitecture.FunctionalTests# Angular Unit Tests
cd CleanArchitecture.Presentation.Web.Client
ng test
# Angular E2E Tests
ng e2eTests include:
- Command/Query handlers
- Validation logic
- Entity Framework operations
- API endpoint functionality
- Authentication/Authorization
- Angular components and services
- JWT Bearer tokens with configurable expiration
- Secure password hashing using Identity framework
- Token refresh capabilities
- Angular authentication guards
- Claims-based permissions
- API endpoint protection
- Route guards in Angular
- SQL injection prevention via parameterized queries
- CORS configuration for controlled access
- Input validation at multiple layers
- Error handling without sensitive data exposure
- XSS protection in Angular
- Update connection strings for production database
- Configure JWT settings with production-grade keys
- Set up proper logging configuration
- Configure CORS for production domains
- Enable HTTPS for secure communication
- Build Angular app for production
cd CleanArchitecture.Presentation.Web.Client
ng build --configuration productionDocker configuration can be added for containerized deployment of both API and Angular app.
Built-in health checks at /health endpoint for monitoring.
- 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
- Follow Clean Architecture principles
- Write comprehensive unit tests
- Use meaningful commit messages
- Document public APIs
- Follow C# and Angular coding conventions
This template intentionally does not use the Repository Pattern because:
- Entity Framework is already a Repository - DbContext provides the repository functionality
- Avoid over-abstraction - Additional repository layer adds complexity without benefits
- Direct EF access - CQRS handlers work directly with DbContext for better performance
- Testability - EF Core's in-memory provider allows easy testing without mocking
- LINQ support - Direct access to IQueryable for complex queries
// Command Handler Example
public class CreateBookCommandHandler : IRequestHandler<CreateBookRequest, Response<int>>
{
private readonly IApplicationDbContext _context;
public async Task<Response<int>> Handle(CreateBookRequest request, CancellationToken cancellationToken)
{
var book = new Book { /* properties */ };
_context.Books.Add(book);
await _context.SaveChangesAsync(cancellationToken);
return Response<int>.Success(book.Id);
}
}This project is licensed under the MIT License - see the LICENSE file for details.
- Clean Architecture by Robert C. Martin
- MediatR for CQRS implementation
- Entity Framework Core for data access
- Angular and Angular CLI for frontend framework
- PrimeNG for UI components
- Serilog for structured logging
- FluentValidation for validation logic
- xUnit and FluentAssertions for testing
- Sakai web template
For support and questions:
- Create an issue in the GitHub repository
- Check the documentation in the wiki
- Review the test projects for usage examples
Built with β€οΈ using .NET 9, Angular 16, and Clean Architecture principles