CleanArchSharp

NuGet Downloads

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

Terminal
dotnet add package CleanArchSharp

Quick Start

Create a New Project

1

Install the template

Terminal window
dotnet new install CleanArchSharp
2

Create a new project

Terminal window
dotnet new cleanarch -n MyProject -o MyProject
3

Navigate to the project

Terminal window
cd MyProject
4

Run the project

Terminal window
dotnet run

Template Options

OptionDescriptionDefault
-n, --nameProject nameRequired
-o, --outputOutput directorySame as name
--databaseDatabase typeSqlServer
--loggingLogging providerSerilog
--authAuthentication typeJwt

Database Options

Choose your preferred database:

Terminal window
# SQL Server (default)
dotnet new cleanarch -n MyProject --database SqlServer
# PostgreSQL
dotnet new cleanarch -n MyProject --database Postgres
# SQLite
dotnet new cleanarch -n MyProject --database Sqlite
# No database
dotnet new cleanarch -n MyProject --database None

Project 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.sln

Each 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:

MethodEndpointDescription
GET/api/healthHealth check
GET/api/weatherforecastSample endpoint
POST/api/auth/loginUser login
POST/api/auth/registerUser registration

Dependency Injection

Services are registered in Program.cs:

// Domain Services
services.AddScoped<IUserRepository, UserRepository>();
// Application Services
services.AddScoped<IUserService, UserService>();
// Infrastructure Services
services.AddScoped<IEmailService, EmailService>();

Testing

The template includes test projects with xUnit and Moq:

Terminal window
# Run all tests
dotnet test
# Run with coverage
dotnet 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:

Terminal window
# Build the image
docker build -t myproject .
# Run the container
docker run -p 5000:80 myproject

FAQ

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

Support