A clean architecture .NET 9 Web API project demonstrating best practices for building scalable and maintainable RESTful services.
It will show how we interface with a frontend and an external service to provide weather forecast data. Front-end will be developed in a different repo.
CoreAPI is a multi-layered application structured to separate concerns and promote maintainability:
- CoreApi - Web API layer containing controllers, managers, and API models
- CoreDAL - Data Access Layer with repositories and Entity Framework Core implementation
- CoreDomain - Domain models and business entities
The project follows a layered architecture pattern:
-
API Layer (CoreApi)
- Controllers: Handle HTTP requests/responses
- Managers: Business logic and orchestration
- Response Models: API-specific DTOs
- Extensions: Service configuration helpers
-
Data Access Layer (CoreDAL)
- Repositories: Generic repository pattern implementation
- Unit of Work: Transaction management
- DbContext: Entity Framework Core database context
-
Domain Layer (CoreDomain)
- Domain Models: Core business entities
- .NET 9 - Latest .NET framework
- ASP.NET Core - Web API framework
- Entity Framework Core - ORM for database access
- PostgreSQL - Database (via Npgsql provider)
- OpenAPI - API documentation
- .NET 9 SDK
- PostgreSQL database
Update the connection string in appsettings.json:
{
"ConnectionStrings": {
"WeatherDbContext": "Host=localhost;Database=weatherdb;Username=your_user;Password=your_password"
}
}- Restore dependencies:
dotnet restore- Run the application:
dotnet run- Access OpenAPI documentation (Development mode):
https://localhost:{port}/openapi/v1.json
CoreApi/
├── Controllers/ # API endpoints
├── Managers/ # Business logic layer
│ └── Interfaces/ # Manager contracts
├── Models/
│ └── Responses/ # API response DTOs
├── Extensions/ # Service configuration
└── Program.cs # Application entry point
CoreDAL/
├── Data/ # DbContext and data configuration
├── Repository/ # Repository pattern implementation
│ └── Interfaces/ # Repository contracts
└── UnitOfWork/ # Transaction management
CoreDomain/
└── Models/ # Domain entities
- GET
/WeatherForecast- Retrieve weather forecasts
- Repository Pattern - Abstracts data access logic
- Unit of Work Pattern - Manages transactions across repositories
- Dependency Injection - Promotes loose coupling and testability
- Generic Repository - Reusable CRUD operations for entities
- Create domain model in
CoreDomain - Add repository interface and implementation in
CoreDAL - Create manager interface and implementation in
CoreApi/Managers - Build controller in
CoreApi/Controllers - Register services in
ServiceCollectionExtensions
- Implement auth
- Implement httpRequests API Service call to https://open-meteo.com/
- Implement endpoints for frontend to include https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m&format=json&timeformat=unixtime
- Docker Container debugging and testings