A simple .NET 9 Web API project following the Clean Architecture pattern.
- ASP.NET Core Web API
- Entity Framework Core (Code First)
- PostgreSQL
- MediatR
- Clean Architecture
clean/
│
├── clean.api/ # Startup project - Entry point of the app
│ └── Controllers/ # API endpoint definitions
│
├── clean.application/ # Application layer (CQRS, business logic)
│ ├── Features/ # Use case logic (Commands, Queries, Handlers)
│ └── Contracts/ # Abstractions (e.g. IRepository)
│
├── clean.domain/ # Domain layer - business entities
│ └── Entities/
│
├── clean.persistence/ # Infrastructure layer (EF Core, DB config)
│ ├── AppDbContext.cs
│ ├── Configurations/
│ └── Repositories/ # Repository implementations
│
├── clean.infrastructure/ # Dependency injection and other infra services
│ └── DependencyInjection.cs # Register services into DI container
-
Run the API:
cd clean.api dotnet run -
Access Swagger UI: Open your browser and go to: http://localhost:5111/swagger/index.html
-
Add a new Controller:
- Create a new file in
clean.api/Controllers/.
- Create a new file in
-
Add a new Feature:
- Define
Command,Query, andHandlerinsideclean.application/Features/{EntityName}/.
- Define
-
Define Contract Interface:
- Create
I{Entity}Repositoryinsideclean.application/Contracts/.
- Create
-
Implement Repository:
- Implement it in
clean.persistence/Repositories/.
- Implement it in
-
Define Entity:
- Define model in
clean.domain/Entities/.
- Define model in
-
Configure Entity Mapping:
- Configure DB mapping in
clean.persistence/Configurations/.
- Configure DB mapping in
To create migrations:
dotnet ef migrations add AddUserAndRoleTables \
--project ./clean.persistence \
--startup-project ./clean.apiTo apply migrations to DB:
dotnet ef database update \
--project ./clean.persistence \
--startup-project ./clean.api-
This project uses EF Core Code First.
-
Make sure to install the
Microsoft.EntityFrameworkCore.Designpackage in theclean.persistenceproject:dotnet add clean.persistence package Microsoft.EntityFrameworkCore.Design
MIT