CleanArchSharp
A Clean Architecture template for .NET WebAPI projects with customizable database and logging options.
Features
- Clean Architecture structure with separation of concerns
- Configurable database options (SQL Server, PostgreSQL, SQLite)
- Built-in logging with Serilog
- JWT Authentication setup
- Swagger/OpenAPI integration
- Docker support out of the box
- CI/CD ready GitHub Actions workflow
Installation
dotnet add package CleanArchSharpQuick Start
Create a New Project
Install the template
dotnet new install CleanArchSharpCreate a new project
dotnet new cleanarch -n MyProject -o MyProjectNavigate to the project
cd MyProjectRun the project
dotnet runTemplate Options
| Option | Description | Default |
|---|---|---|
-n, --name | Project name | Required |
-o, --output | Output directory | Same as name |
--database | Database type | SqlServer |
--logging | Logging provider | Serilog |
--auth | Authentication type | Jwt |
Database Options
Choose your preferred database:
# SQL Server (default)dotnet new cleanarch -n MyProject --database SqlServer
# PostgreSQLdotnet new cleanarch -n MyProject --database Postgres
# SQLitedotnet new cleanarch -n MyProject --database Sqlite
# No databasedotnet new cleanarch -n MyProject --database NoneProject Structure
The generated project follows Clean Architecture principles:
MyProject/├── src/│ ├── MyProject.Domain/ # Entities, Value Objects, Interfaces│ ├── MyProject.Application/ # Use Cases, Business Logic, DTOs│ ├── MyProject.Infrastructure/ # Data Access, External Services│ └── MyProject.WebAPI/ # API Controllers, Middleware├── tests/│ ├── MyProject.UnitTests/│ └── MyProject.IntegrationTests/└── MyProject.slnEach layer has a clear responsibility. Domain has no dependencies, Application depends only on Domain, and so on.
Configuration
appsettings.json
The template includes sensible defaults:
{ "ConnectionStrings": { "DefaultConnection": "Server=localhost;Database=MyProjectDb;Trusted_Connection=true" }, "JwtSettings": { "Secret": "Your-Secret-Key-Here", "Issuer": "MyProject", "Audience": "MyProject", "ExpirationInMinutes": 60 }, "Serilog": { "MinimumLevel": { "Default": "Information" } }}Never commit secrets to source control. Use User Secrets, environment variables, or Azure Key Vault for production.
API Endpoints
The template includes sample endpoints:
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/health | Health check |
| GET | /api/weatherforecast | Sample endpoint |
| POST | /api/auth/login | User login |
| POST | /api/auth/register | User registration |
Dependency Injection
Services are registered in Program.cs:
// Domain Servicesservices.AddScoped<IUserRepository, UserRepository>();
// Application Servicesservices.AddScoped<IUserService, UserService>();
// Infrastructure Servicesservices.AddScoped<IEmailService, EmailService>();Testing
The template includes test projects with xUnit and Moq:
# Run all testsdotnet test
# Run with coveragedotnet test --collect:"XPlat Code Coverage"Tests are organized following the same layer structure. Unit tests focus on Application layer, while Integration tests cover the WebAPI.
Docker Support
Build and run with Docker:
# Build the imagedocker build -t myproject .
# Run the containerdocker run -p 5000:80 myprojectFAQ
Can I use this with .NET 8?
Yes! The template targets .NET 8 by default and supports .NET 6+.
How do I add MediatR?
MediatR is already included in the Application layer. Just create your commands and handlers:
public record CreateUserCommand(string Name, string Email) : IRequest<User>;
public class CreateUserCommandHandler : IRequestHandler<CreateUserCommand, User>{ private readonly IUserRepository _userRepository;
public CreateUserCommandHandler(IUserRepository userRepository) { _userRepository = userRepository; }
public async Task<User> Handle(CreateUserCommand request, CancellationToken cancellationToken) { var user = new User(request.Name, request.Email); await _userRepository.AddAsync(user); return user; }}Can I customize the template?
Absolutely! After generating, modify the code as needed. The template is just a starting point.
Changelog
v2.1.1 (Current)
- Fixed minor bugs in authentication middleware
- Updated NuGet packages to latest versions
v2.1.0
- Added PostgreSQL support
- Improved Docker configuration
v2.0.0
- Upgraded to .NET 8
- Breaking changes in configuration structure