# jamshed34/cleanarchitecturegeneric architecture

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

- Interactive diagram: https://gitdiagram.com/jamshed34/cleanarchitecturegeneric
- Repository: https://github.com/jamshed34/cleanarchitecturegeneric

## Overview

##### Project Type & Purpose
- This is a backend “Clean Architecture” template for a micro-service/monolith API built on .NET 9, implementing IdentityServer4 for authentication, CQRS via MediatR for business logic, and EF Core (SQL Server) for persistence. It uses .NET Aspire (AppHost) as a lightweight service host/orchestrator.

##### Core Layers & Directories
- AppHost (src/AppHost)
- Hosts and wires up all services (IdentityServer, Web API).
- Presentation/WebApi (src/Presentation/WebApi)
- Exposes HTTP endpoints (Controllers, Swagger).
- Application (src/Application)
- Business logic: Commands, Queries, DTOs, AutoMapper mappings, MediatR handlers; registered via ApplicationServiceRegistration.
- Domain (src/Domain)
- Core entities (User), interfaces (IUserRepository, IUnitOfWork).
- Infrastructure (src/Infrastructure)
- Data (ApplicationDbContext, EF Migrations, Seed Data), Identity (IdentityServer4 config & migrations), Repositories (UserRepository), plus service registrations (InfrastructureServiceRegistration).
- tests (UnitTests & IntegrationTests)

##### External Dependencies
- SQL Server (AspNetUsers, AspNetRoles, Clients, PersistedGrants)
- External clients (web front-ends, Postman, mobile apps) consuming the API
- IdentityServer4 endpoints (/connect/token, /.well-known/openid-configuration)

##### Architectural Patterns & Principles
- Clean Architecture (onion/hexagonal layering): outer layers depend on inner abstractions only.
- CQRS: separate Commands (CreateUserCommand) and Queries (GetUserByIdQuery) handled by MediatR.
- Repository + Unit of Work: Domain interfaces implemented by Infrastructure.
- DI & Orchestration: .NET Aspire in AppHost configures all layers centrally.

- External Clients → (HTTPS + OAuth2) → IdentityServer4 (in AppHost/Infrastructure/Identity)
- External Clients → (Bearer Token) → WebApi Controllers
- Controllers → MediatR → Application Handlers (Commands/Queries)
- Application Handlers → Domain Interfaces (IUserRepository, IUnitOfWork)
- Infrastructure Repositories → EF Core → SQL Server
- AppHost → DI Container → wires Presentation, Application, Infrastructure, IdentityServer

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients
    subgraph "External Clients"
        Clients["Web/Mobile/Postman"]:::external
    end

    %% AppHost Layer
    subgraph "AppHost (.NET Aspire)" 
        IS4["IdentityServer4"]:::identity
        WebApi["WebApi (ASP.NET Core)"]:::presentation
    end

    %% Application Layer
    subgraph "Application Layer"
        RegistrationApp["ApplicationServiceRegistration"]:::application
        Commands["CreateUserCommand"]:::application
        Queries["GetUserByIdQuery"]:::application
        DTO["UserDto"]:::application
        Maps["MappingProfile (AutoMapper)"]:::application
    end

    %% Domain Layer
    subgraph "Domain Layer"
        UserEntity["User"]:::domain
        IUserRepo["IUserRepository"]:::domain
        IUnit["IUnitOfWork"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        InfraReg["InfrastructureServiceRegistration"]:::infrastructure
        DbContext["ApplicationDbContext"]:::infrastructure
        MIGData["Data Migrations"]:::infrastructure
        EFSeed["SeedData"]:::infrastructure
        IdentityCfg["IdentityServerConfig & Setup"]:::infrastructure
        MIGIdentity["Identity Migrations"]:::infrastructure
        Repo["UserRepository"]:::infrastructure
    end

    %% Persistence
    Sql["SQL Server"]:::database

    %% Test Projects
    subgraph "Tests"
        UT["UnitTests"]:::tests
        IT["IntegrationTests"]:::tests
    end

    %% Connections
    Clients -->|"HTTPS + OAuth2"| IS4
    Clients -->|"Bearer Token"| WebApi

    WebApi -->|"MediatR Send()"| Commands
    WebApi -->|"MediatR Send()"| Queries

    Commands -->|"uses"| IUserRepo
    Queries -->|"uses"| IUserRepo
    Commands -->|"uses"| IUnit
    Queries -->|"uses"| IUnit

    IUserRepo -->|"implemented by"| Repo
    IUnit -->|"implemented by"| InfraReg

    Repo -->|"EF Core"| Sql
    DbContext -->|"EF Core Migrations & Context"| Sql
    MIGData --> DbContext
    EFSeed --> DbContext

    MIGIdentity --> IdentityCfg
    IdentityCfg --> Sql

    RegistrationApp -->|"registers Application services"| WebApi
    InfraReg -->|"registers Infrastructure services"| WebApi

    UT --> ApplicationLayer
    UT --> DomainLayer
    IT --> ApplicationLayer
    IT --> InfrastructureLayer

    %% Click Events
    click WebApi "https://github.com/jamshed34/cleanarchitecturegeneric/tree/main/src/Presentation/WebApi"
    click Clients "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Presentation/WebApi/Controllers/UserController.cs"
    click WebApi "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Presentation/WebApi/Program.cs"
    click WebApi "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Presentation/WebApi/appsettings.json"
    click WebApi "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Presentation/WebApi/appsettings.Development.json"

    click RegistrationApp "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Application/ApplicationServiceRegistration.cs"
    click Commands "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Application/Commands/CreateUserCommand.cs"
    click Queries "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Application/Queries/GetUserByIdQuery.cs"
    click Maps "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Application/Mappings/MappingProfile.cs"
    click DTO "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Application/Models/UserDto.cs"

    click UserEntity "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Domain/Entities/User.cs"
    click IUserRepo "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Domain/Interfaces/IUserRepository.cs"
    click IUnit "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Domain/Interfaces/IUnitOfWork.cs"

    click InfraReg "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Infrastructure/InfrastructureServiceRegistration.cs"
    click DbContext "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Infrastructure/Data/ApplicationDbContext.cs"
    click MIGData "https://github.com/jamshed34/cleanarchitecturegeneric/tree/main/src/Infrastructure/Data/Migrations"
    click EFSeed "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Infrastructure/Data/SeedData.cs"
    click IdentityCfg "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Infrastructure/Identity/IdentityServerConfig.cs"
    click IdentityCfg "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Infrastructure/Identity/IdentityServerSetup.cs"
    click MIGIdentity "https://github.com/jamshed34/cleanarchitecturegeneric/tree/main/src/Infrastructure/Identity/Migrations"
    click Repo "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/src/Infrastructure/Repositories/UserRepository.cs"

    click IT "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/tests/IntegrationTests/IntegrationTests.csproj"
    click IT "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/tests/IntegrationTests/UnitTest1.cs"
    click UT "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/tests/UnitTests/UnitTests.csproj"
    click UT "https://github.com/jamshed34/cleanarchitecturegeneric/blob/main/tests/UnitTests/UnitTest1.cs"

    %% Styles
    classDef external fill:#cccccc,stroke:#333,stroke-width:1px;
    classDef host fill:#bb99ff,stroke:#333,stroke-width:1px;
    classDef presentation fill:#99ccff,stroke:#333,stroke-width:1px;
    classDef application fill:#99ff99,stroke:#333,stroke-width:1px;
    classDef domain fill:#ffff99,stroke:#333,stroke-width:1px;
    classDef infrastructure fill:#ffcc99,stroke:#333,stroke-width:1px;
    classDef database fill:#d9b3ff,stroke:#333,stroke-width:1px;
    classDef tests fill:#ff99cc,stroke:#333,stroke-width:1px;

    class WebApi,IS4 host
    class ApplicationLayer application
    class DomainLayer domain
    class InfrastructureLayer infrastructure
    class Sql database
    class UT,IT tests
```
