Skip to content

paulbenjamin3409/csharpApi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CoreAPI - .NET 9 Web API

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.

Project Overview

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

Architecture

The project follows a layered architecture pattern:

Layers

  1. API Layer (CoreApi)

    • Controllers: Handle HTTP requests/responses
    • Managers: Business logic and orchestration
    • Response Models: API-specific DTOs
    • Extensions: Service configuration helpers
  2. Data Access Layer (CoreDAL)

    • Repositories: Generic repository pattern implementation
    • Unit of Work: Transaction management
    • DbContext: Entity Framework Core database context
  3. Domain Layer (CoreDomain)

    • Domain Models: Core business entities

Technologies

  • .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

Getting Started

Prerequisites

  • .NET 9 SDK
  • PostgreSQL database

Configuration

Update the connection string in appsettings.json:

{
  "ConnectionStrings": {
    "WeatherDbContext": "Host=localhost;Database=weatherdb;Username=your_user;Password=your_password"
  }
}

Running the Application

  1. Restore dependencies:
   dotnet restore
  1. Run the application:
   dotnet run
  1. Access OpenAPI documentation (Development mode):
   https://localhost:{port}/openapi/v1.json

Project Structure

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

API Endpoints

Weather Forecast

  • GET /WeatherForecast - Retrieve weather forecasts

Design Patterns

  • 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

Development

Adding New Features

  1. Create domain model in CoreDomain
  2. Add repository interface and implementation in CoreDAL
  3. Create manager interface and implementation in CoreApi/Managers
  4. Build controller in CoreApi/Controllers
  5. Register services in ServiceCollectionExtensions

TODO

About

Clean architecture .NET 9 Web Api

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published