This project is a Clean Architecture template for ASP.NET, designed to provide a robust and maintainable solution architecture.
The solution is organized into four primary projects:
- Domain Project: Contains core domain entities, value objects, and domain-specific logic
- Application Project: Implements business logic, DTOs, interfaces, and application services
- Infrastructure Project: Manages external concerns like database access, external service integrations
- API Project: Hosts minimal APIs, controllers, and application entry point
- ASP.NET
- Entity Framework Core
- AutoMapper
- Minimal APIs
- FluentResult and FluentValidations
- Repository Pattern
- Clean Architecture
- Dependency Inversion
- Separation of Concerns
- .NET 8 SDK
- Visual Studio 2022 or JetBrains Rider
- SQL Server or PostgreSQL
- Clone the repository
git [email protected]:marcosagni98/dotnet-api-template.git
- Navigate to the project directory
cd dotnet-api-template
- Restore NuGet packages
dotnet restore
-
Configure connection string in
appsettings.json
-
Run database migrations
dotnet ef database update
- Run the application
dotnet run
├── src
│ ├── YourProject.API
│ │ ├── Endpoints
│ │ └── Program.cs
│ ├── YourProject.Application
│ │ ├── DTOs
│ │ ├── Interfaces
│ │ └── Services
│ ├── YourProject.Domain
│ │ ├── Entities
│ │ ├── Interfaces
│ │ └── ValueObjects
│ └── YourProject.Infrastructure
│ ├── Data
│ ├── Repositories
│ └── Services
public class UserEndpoints
{
public static void MapUserEndpoints(this IEndpointRouteBuilder app)
{
app.MapGet("/users", async (IUserService userService) =>
{
var users = await userService.GetAllUsersAsync();
return Results.Ok(users);
})
.Produces<IEnumerable<UserDto>>(200)
.WithName("GetUsers")
.WithTags("Users");
app.MapPost("/users", async (CreateUserDto userDto, IUserService userService) =>
{
var createdUser = await userService.CreateUserAsync(userDto);
return Results.CreatedAtRoute("GetUserById", new { id = createdUser.Id }, createdUser);
})
.Produces<UserDto>(201)
.WithName("CreateUser")
.WithTags("Users");
}
}
Configure services in Program.cs
:
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddAutoMapper(typeof(Program));
// Map endpoints
app.MapUserEndpoints();
public interface IUserRepository
{
Task<User> GetByIdAsync(int id);
Task<IEnumerable<User>> GetAllAsync();
Task AddAsync(User user);
Task UpdateAsync(User user);
Task DeleteAsync(int id);
}
- Unit Tests: Located in a separate test project
- Uses xUnit or NUnit
- Covers domain logic, repositories, and services
Configured with:
- Microsoft.Extensions.Logging
OpenAPI is configured for API documentation and testing.
- Dockerfile included
- Docker Compose configuration available
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.