# aawwad172/dotnet-template architecture

> Architecture diagram of the GitHub repository aawwad172/dotnet-template, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aawwad172/dotnet-template
- Repository: https://github.com/aawwad172/dotnet-template

## Overview

##### Project Type & Purpose
- This is a .NET template meant for quickly setting up a robust application using modern best practices such as DDD, Clean Architecture, CQRS, and UnitOfWork patterns.
- The solution is split across four main layers: Domain, Application, Infrastructure, and Presentation. Its purpose is to serve as a scaffolding for building feature-rich web APIs integrated with PostgreSQL and secured via JWT authentication.

##### Major Components
- Contains core business entities (User, RefreshToken, etc.), enums, and custom exceptions.
- Emphasizes the business logic of the application.
- Contains the CQRS implementation including command handlers (Authentication handlers: Login, Logout, RefreshToken, RegisterUser), commands, interfaces, and helper services (e.g., AuthenticationService, JwtService).
- Also includes dependency injection setup for decoupling components.
- Implements data access logic using EF Core, repositories (generic and custom), and database context along with migrations for PostgreSQL.
- Contains additional configurations and utility extensions (e.g., PostgresEnumExtensions, pagination queries).
- Provides the Web API project including middleware layers (JWT authentication middleware, ExceptionHandlerMiddleware for global exception handling), routes, validators, and API models.
- Contains configuration files (appsettings.json) that enable database connections, JWT settings, and other security-related configurations.

##### Relationships and Interactions
- The Domain layer represents the core business models and is independent from the rest.
- The Application layer orchestrates use-case logic via CQRS command and query handlers and calls upon Domain services and entities.
- The Infrastructure layer provides implementations that support the application (e.g., repositories for data persistence) and interacts directly with the data store.
- The Presentation layer is the entry point for external clients, performing request validations, invoking Application layer services, and handling HTTP responses.
- How the Presentation layer calls the Application layer.
- How the Application layer leverages Domain for business rules and Infrastructure for data persistence.
- How cross-cutting concerns (JWT authentication, exception handling, and Fluent Validation) are integrated.

##### Architectural Patterns and Important Technology Elements
- DDD and Clean Architecture by segregating concerns.
- CQRS and UnitOfWork patterns found in the Application layer.
- Technologies such as:
- .NET (C#) for the overall platform.
- EF Core and PostgreSQL for data persistence.
- JWT for authentication, accompanied by a dedicated JWT service.
- Fluent Validation for command and API input validation.
- Middleware components that handle exception processing and logging (via repositories and ApiResponse classes).
- Usage of Husky (for pre-commit hooks) and Makefile for .NET CLI tasks, though these are more related to development processes rather than runtime architecture.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        P_API["Web API & Configurations"]:::presentation
        P_MW["Middleware (JWT&ExceptionHandler)"]:::presentation
        P_Routes["Authentication Routes"]:::presentation
        P_Validators["Command Validators (Authentication)"]:::presentation
        P_DI["Dependency Injection"]:::presentation
    end

    %% Application Layer
    subgraph "Application Layer"
        APP_CQRS["CQRS Command Handlers (Authentication)"]:::application
        APP_Commands["CQRS Commands (Authentication)"]:::application
        APP_Helper["Helper Services (JWT,Encryption)"]:::application
        APP_Interfaces["Service Interfaces"]:::application
        APP_DI["Dependency Injection Setup"]:::application
    end

    %% Domain Layer
    subgraph "Domain Layer"
        DOM_Main["Domain Entities, Enums, and Exceptions"]:::domain
        DOM_Entities["Entities"]:::domain
        DOM_Enums["Enums"]:::domain
        DOM_Exceptions["Exceptions"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        INF_Repo["Database Context & Repositories"]:::infrastructure
        INF_Migrations["Migrations"]:::infrastructure
        INF_Extensions["Postgres Enum Extensions"]:::infrastructure
        INF_Configs["Configurations"]:::infrastructure
    end

    %% Relationships
    P_API -->|"calls"| APP_CQRS
    P_Routes -->|"calls"| APP_CQRS
    P_API -->|"calls"| APP_Commands
    P_DI -->|"configures"| APP_DI

    APP_CQRS -->|"uses"| DOM_Main
    APP_Commands -->|"uses"| DOM_Main
    APP_CQRS -->|"persists"| INF_Repo
    APP_Helper -->|"assists"| INF_Repo

    %% Composition within Domain Layer
    DOM_Main ---|"includes"| DOM_Entities
    DOM_Main ---|"includes"| DOM_Enums
    DOM_Main ---|"includes"| DOM_Exceptions

    %% Composition within Infrastructure Layer
    INF_Repo ---|"includes"| INF_Migrations
    INF_Repo ---|"includes"| INF_Extensions
    INF_Repo ---|"includes"| INF_Configs

    %% Styles for layers
    classDef domain fill:#00CED1,stroke:#000,stroke-width:2px;
    classDef application fill:#32CD32,stroke:#000,stroke-width:2px;
    classDef infrastructure fill:#1E90FF,stroke:#000,stroke-width:2px;
    classDef presentation fill:#FFA500,stroke:#000,stroke-width:2px;

    %% Click Events - Domain Layer
    click DOM_Main "https://github.com/aawwad172/dotnet-template/blob/main/src/Dotnet.Template.Domain"
    click DOM_Entities "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Domain/Entities"
    click DOM_Enums "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Domain/Enums"
    click DOM_Exceptions "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Domain/Exceptions"

    %% Click Events - Application Layer
    click APP_CQRS "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Application/CQRS/CommandHandlers/Authentication"
    click APP_Commands "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Application/CQRS/Commands/Authentication"
    click APP_Helper "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Application/HelperServices"
    click APP_Interfaces "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Application/Interfaces"
    click APP_DI "https://github.com/aawwad172/dotnet-template/blob/main/src/Dotnet.Template.Application/DependencyInjection.cs"

    %% Click Events - Infrastructure Layer
    click INF_Repo "https://github.com/aawwad172/dotnet-template/blob/main/src/Dotnet.Template.Infrastructure"
    click INF_Migrations "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Infrastructure/Migrations"
    click INF_Extensions "https://github.com/aawwad172/dotnet-template/blob/main/src/Dotnet.Template.Infrastructure/Extensions/PostgresEnumExtensions.cs"
    click INF_Configs "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Infrastructure/Configurations"

    %% Click Events - Presentation Layer
    click P_API "https://github.com/aawwad172/dotnet-template/blob/main/src/Dotnet.Template.Presentation.API"
    click P_MW "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Presentation.API/Middlewares"
    click P_Routes "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Presentation.API/Routes/Authentication"
    click P_Validators "https://github.com/aawwad172/dotnet-template/tree/main/src/Dotnet.Template.Presentation.API/Validators/Commands/Authentication"
    click P_DI "https://github.com/aawwad172/dotnet-template/blob/main/src/Dotnet.Template.Presentation.API/DependencyInjection.cs"
```
