# jgmelon22/studentsnhibernatedemo architecture

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

- Interactive diagram: https://gitdiagram.com/jgmelon22/studentsnhibernatedemo
- Repository: https://github.com/jgmelon22/studentsnhibernatedemo

## Overview

##### Project Type and Purpose
- This is a server-side .NET layered API project (“NHibernate Students API”) built to demonstrate DDD and the Mediator pattern using NHibernate as the ORM against a SQL Server database.
- No UI layer; it exposes HTTP endpoints (Swagger/OpenAPI) for CRUD operations on a Student aggregate.

##### High-Level File Structure and Architectural Layers
- **Presentation/API Layer** (`src/NHibernateDemo.API`)
- HTTP endpoints (StudentEndpoint.cs)
- Middleware (BasicAuthMiddleware)
- Swagger setup, DI configuration (IocExtensions, SwaggerCollection)
- **Application Layer** (`src/NHibernateDemo.Application`)
- Commands & Queries + their Handlers (CreateStudentCommandHandler, GetStudentsQueryHandler, etc.)
- Implements the Mediator pattern (NetDevPack.SimpleMediator)
- **Domain/Core Layer** (`src/NHibernateDemo.Core`)
- Entities (Student)
- DTOs (StudentRequest, StudentResponse)
- Mapping extensions, shared Result type
- **Infrastructure Layer** (`src/NHibernateDemo.Infrastructure`)
- NHibernate configuration & Fluent mappings (StudentMap)
- Repository interfaces & implementations (IStudentRepository → StudentRepository)
- Database connectivity configuration (BasicAuthOptions, NHibernateConfiguration)
- **Database** (`/SQL/CreateDatabaseScript.sql`)
- SQL Server schema creation
- **Unit Tests** (`/tests/UnitTests`)
- Layer-specific tests (API endpoints, middleware, application commands/queries)

##### Main Components and Their Relationships
- **API (ASP.NET Core)**
→ Invokes **Application** via Mediator (commands/queries)
- **Application**
→ Depends on **Core** for DTOs/entities
→ Depends on **Infrastructure** via repository interfaces
- **Infrastructure**
→ Implements repository interfaces using **NHibernate** & **FluentNHibernate**
→ Connects to **SQL Server**
- **Database (SQL Server)**
← Receives generated SQL from NHibernate sessions
- **Middleware** (BasicAuth) sits in the pipeline before controllers
- **Swagger** exposes the HTTP contract

##### Architectural Patterns and Design Principles
- **Layered (Clean) Architecture**: clear boundaries between API, Application, Domain, Infrastructure
- **Mediator Pattern**: decouples controllers from business logic via commands & queries
- **Dependency Injection**: appsettings.json + IocExtensions register services at startup
- **Domain-Driven Design (DDD)**: Entities/mappings in Core, repositories in Infrastructure
- **Separation of Concerns**: No data-access code in controllers, no HTTP code in domain

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% API Layer
    subgraph "API Layer"
        direction TB
        UI["Swagger UI"]:::api
        Auth["BasicAuthMiddleware"]:::api
        Endpoint["StudentEndpoint"]:::api
        Program["Program"]:::api
        IocExt["IocExtensions"]:::api
        SwaggerCol["SwaggerCollection"]:::api
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        Mediator(("Mediator")):::app
        CSCmd["CreateStudendCommand"]:::app
        CSCmdH["CreateStudendCommandHandler"]:::app
        USCmd["UpdateStudentCommand"]:::app
        USCmdH["UpdateStudentCommandHandler"]:::app
        RSCmd["RemoveStudentCommand"]:::app
        RSCmdH["RemoveStudentCommandHandler"]:::app
        GSQuery["GetStudentsQuery"]:::app
        GSQueryH["GetStudentsQueryHandler"]:::app
        GSBIdQuery["GetStudentByIdQuery"]:::app
        GSBIdQueryH["GetStudentByIdQueryHandler"]:::app
    end

    %% Core Layer
    subgraph "Core Layer"
        direction TB
        Entity["Student Entity"]:::core
        ReqDTO["StudentRequest DTO"]:::core
        ResDTO["StudentResponse DTO"]:::core
        MapExt["MappingExtensions"]:::core
        ResultType["Result Type"]:::core
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        direction TB
        NHConfig["NHibernateConfiguration"]:::infra
        AuthOpt["BasicAuthOptions"]:::infra
        IRepo["IStudentRepository"]:::infra
        RepoImpl["StudentRepository"]:::infra
        FluentMap["StudentMap"]:::infra
    end

    %% Database
    subgraph "Database"
        direction TB
        SQLServer[(SQL Server)]:::db
        DBScript["CreateDatabaseScript.sql"]:::db
    end

    %% Relationships
    UI -->|"HTTP Request"| Auth
    Auth -->|"Validate"| Endpoint
    Endpoint -->|"ICommand/IQuery"| Mediator
    Mediator -->|"ICommand"| CSCmdH
    Mediator -->|"IQuery"| GSQueryH
    Mediator -->|"ICommand"| USCmdH
    Mediator -->|"ICommand"| RSCmdH
    Mediator -->|"IQuery"| GSBIdQueryH

    CSCmdH -->|uses| CSCmd
    USCmdH -->|uses| USCmd
    RSCmdH -->|uses| RSCmd
    GSQueryH -->|uses| GSQuery
    GSBIdQueryH -->|uses| GSBIdQuery

    CSCmdH -->|DTO/Entity| Entity
    CSCmdH -->|DTO| ReqDTO
    GSQueryH -->|DTO| ResDTO
    GSBIdQueryH -->|DTO| ResDTO

    CSCmdH -->|"IStudentRepository"| IRepo
    USCmdH -->|"IStudentRepository"| IRepo
    RSCmdH -->|"IStudentRepository"| IRepo
    GSQueryH -->|"IStudentRepository"| IRepo
    GSBIdQueryH -->|"IStudentRepository"| IRepo

    IRepo -->|"implemented by"| RepoImpl
    RepoImpl -->|uses| NHConfig
    RepoImpl -->|uses| FluentMap
    RepoImpl -->|"NHibernate Session"| SQLServer
    SQLServer -->|"runs script"| DBScript

    %% Click Events
    click Endpoint "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.API/Endpoints/StudentEndpoint.cs"
    click Auth "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.API/Middleware/BasicAuthMiddleware.cs"
    click IocExt "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.API/Extensions/IocExtensions.cs"
    click SwaggerCol "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.API/Extensions/SwaggerCollection.cs"
    click Program "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.API/Program.cs"
    click CSCmd "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Application/Commands/CreateStudendCommand.cs"
    click CSCmdH "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Application/Commands/Handlers/CreateStudendCommandHandler.cs"
    click USCmd "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Application/Commands/UpdateStudentCommand.cs"
    click USCmdH "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Application/Commands/Handlers/UpdateStudentCommandHandler.cs"
    click RSCmd "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Application/Commands/RemoveStudentCommand.cs"
    click RSCmdH "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Application/Commands/Handlers/RemoveStudentCommandHandler.cs"
    click GSQuery "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Application/Queries/GetStudentsQuery.cs"
    click GSQueryH "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Application/Queries/Handlers/GetStudentsQueryHandler.cs"
    click GSBIdQuery "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Application/Queries/GetStudentByIdQuery.cs"
    click GSBIdQueryH "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Application/Queries/Handlers/GetStudentByIdQueryHandler.cs"
    click Entity "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Core/Domains/Entities/Student.cs"
    click ReqDTO "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Core/Domains/DTOs/Requests/StudentRequest.cs"
    click ResDTO "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Core/Domains/DTOs/Responses/StudentResponse.cs"
    click MapExt "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Core/Domains/Mappings/MappingExtensions.cs"
    click ResultType "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Core/Shared/Result.cs"
    click NHConfig "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Infrastructure/Configuration/NHibernateConfiguration.cs"
    click AuthOpt "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Infrastructure/Configuration/BasicAuthOptions.cs"
    click IRepo "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Infrastructure/Interfaces/Repositories/IStudentRepository.cs"
    click RepoImpl "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Infrastructure/Repositories/StudentRepository.cs"
    click FluentMap "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/src/NHibernateDemo.Infrastructure/Mapping/StudentMap.cs"
    click DBScript "https://github.com/jgmelon22/studentsnhibernatedemo/blob/main/SQL/CreateDatabaseScript.sql"

    %% Styles
    classDef api fill:#cce5ff,stroke:#004085,color:#004085;
    classDef app fill:#d4edda,stroke:#155724,color:#155724;
    classDef core fill:#fff3cd,stroke:#856404,color:#856404;
    classDef infra fill:#ffe5b4,stroke:#ff8c00,color:#ff8c00;
    classDef db fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
```
