# premkmr16/tms architecture

> Architecture diagram of the GitHub repository premkmr16/tms, 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/premkmr16/tms
- Repository: https://github.com/premkmr16/tms

## Overview

##### Project Type and Purpose
- This is a monolithic backend API (TMS.API) built with .NET 7 following a Clean/Onion Architecture.
- Purpose: expose HTTP endpoints for managing Employees, EmployeeTypes, WorkItems, Projects, etc., with support for CRUD, Excel import/export, real-time updates (SignalR), and paginated responses.

a. Presentation Layer
- Project: Presentation/TMS.API
- Contains: Minimal HTTP endpoints (EmployeeEndpoints.cs, EmployeeTypeEndpoints.cs), middleware (ApiResponseMiddleware), configuration (Program.cs, appsettings.json)
- Tech: ASP.NET Core Minimal API
b. Application Layer
- Project: Application/TMS.Application
- Contains: CQRS via Mediatr, Features (Commands, Queries, Handlers), Common services (Excel import/export, MappingProfile for AutoMapper, Validation with FluentValidation), Repository interfaces, ApiResponse models
- Tech: Mediatr, AutoMapper, FluentValidation
c. Core (Domain) Layer
- Project: Core/TMS.Core
- Contains: Domain Entities (Employee, WorkItem, Project, etc.), domain-specific error messages, raw SQL queries, endpoint definition helpers
- Tech: Plain C# classes, SQL query strings
d. Infrastructure Layer
- Project: Infrastructure/TMS.Infrastructure
- Contains: EF Core DbContext (TMSDbContext), Migrations, EntityTypeConfigurations, Repository implementations, ConnectionFactory, SignalR Hubs
- Tech: Entity Framework Core, SQL Server provider, SignalR
e. Database
- Folder: Database/StoredProcedures/*.sql
- Contains: Stored procedures for complex queries; underlying relational schema

##### Interactions & Data Flow
1) HTTP Client → Presentation:
- Client issues an HTTP request (e.g., POST /employees).
- Middleware handles cross-cutting concerns (logging, ApiResponse shaping).
2) Presentation → Application:
- Endpoint invokes Mediatr.Send() with a Command or Query DTO.
3) Application Internals:
- Mediatr dispatches to the appropriate Handler (AddEmployeeHandler, GetEmployeesHandler).
- Handler uses Validators (FluentValidation) and Mapping (AutoMapper) from Common.
- Handler calls a Repository Interface (IEmployeeWriteRepository or IEmployeeReadRepository).
4) Application → Infrastructure:
- Repository interface is implemented in Infrastructure.Persistence.Repositories.
- Implementation uses TMSDbContext (EF Core) or raw SQL queries from Core.Database.
5) Infrastructure → Database:
- DbContext issues SQL commands, applies Migrations, executes StoredProcedures where needed.
6) Real-Time Notifications:
- Some handlers push notifications via SignalR Hubs (EmployeeHub) in Infrastructure, broadcasting to connected WebSocket clients.
7) Responses travel back up the stack to the HTTP Client.

##### Architectural Patterns & Principles
- Onion/Clean Architecture: strict dependency rule (outer layers depend inward only on abstractions).
- CQRS + Mediator: separation of Commands (writes) vs. Queries (reads).
- Repository Pattern: abstract data access interfaces in Application, concrete EF Core implementations in Infrastructure.
- Dependency Injection: wiring all layers in Presentation via built-in DI container.
- Layered Error Handling & ApiResponse Wrapping via middleware.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients
    HTTPClient["HTTP Client"]:::external
    SignalRClient["SignalR Client (WebSocket)"]:::external

    %% Presentation Layer
    subgraph "Presentation Layer" 
        direction TB
        Endpoints["Endpoints"]:::presentation
        Middleware["ApiResponseMiddleware"]:::presentation
        Configuration["Program.cs & appsettings.json"]:::presentation
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        Features["CQRS Features"]:::application
        Common["Common Services"]:::application
        ReposInterfaces["Repository Interfaces"]:::application
    end

    %% Core Layer
    subgraph "Core Layer"
        direction TB
        Entities["Entities"]:::core
        Errors["ErrorMessages"]:::core
        QueriesCore["Raw SQL Queries"]:::core
        EndpHelpers["Endpoint Helpers"]:::core
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        direction TB
        Persistence["Persistence"]:::infrastructure
        ConnFactory["ConnectionFactory"]:::infrastructure
        Hubs["SignalR Hubs"]:::infrastructure
    end

    %% Database
    subgraph "Database"
        direction TB
        Tables["Relational Tables"]:::database
        StoredProcs["Stored Procedures"]:::database
    end

    %% Data Flows
    HTTPClient -->|"HTTP Request"| Endpoints
    Endpoints -->|"MediatR.Send()"| Features
    Features -->|uses| ReposInterfaces
    ReposInterfaces -->|implemented by| Persistence
    Persistence -->|EF Core| ConnFactory
    ConnFactory -->|creates| Persistence
    Persistence -->|executes SQL| Tables
    Persistence -->|executes StoredProcedures| StoredProcs
    Features -->|push notification| Hubs
    Hubs -->|WebSocket| SignalRClient
    StoredProcs -->|stored in| Database

    %% Configuration flow
    Configuration -->|reads| ConnFactory

    %% Click Events - Presentation
    click Endpoints "https://github.com/premkmr16/tms/blob/master/Presentation/TMS.API/Endpoints/EmployeeEndpoints.cs"
    click Endpoints "https://github.com/premkmr16/tms/blob/master/Presentation/TMS.API/Endpoints/EmployeeTypeEndpoints.cs"
    click Middleware "https://github.com/premkmr16/tms/blob/master/Presentation/TMS.API/Middlewares/ApiResponseMiddleware.cs"
    click Configuration "https://github.com/premkmr16/tms/blob/master/Presentation/TMS.API/Program.cs"
    click Configuration "https://github.com/premkmr16/tms/blob/master/Presentation/TMS.API/appsettings.json"

    %% Click Events - Application Common
    click Common "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Common/Excel/Handlers/ExportInformationHandler.cs"
    click Common "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Common/Excel/Handlers/ImportInformationHandler.cs"
    click Common "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Common/Mapping/MappingProfile.cs"
    click Common "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Common/Validation/ValidatorRegistration.cs"
    click Common "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Common/Models/ApiResponse.cs"
    click Common "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Common/Models/ErrorResponse.cs"
    click Common "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Common/Models/PaginatedResponse.cs"

    %% Click Events - Application Features & Repos Interfaces
    click Features "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Features/Employees/Commands/Handlers/AddEmployeeHandler.cs"
    click Features "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Features/Employees/Queries/Handlers/GetEmployeesHandler.cs"
    click Features "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Features/Employees/Validators/CreateEmployeeRequestValidator.cs"
    click ReposInterfaces "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Repositories/EmployeeRepository/IEmployeeReadRepository.cs"
    click ReposInterfaces "https://github.com/premkmr16/tms/blob/master/Application/TMS.Application/Repositories/EmployeeRepository/IEmployeeWriteRepository.cs"

    %% Click Events - Core
    click Entities "https://github.com/premkmr16/tms/blob/master/Core/TMS.Core/Entities/Employee.cs"
    click QueriesCore "https://github.com/premkmr16/tms/blob/master/Core/TMS.Core/Database/EmployeeQueries.cs"
    click EndpHelpers "https://github.com/premkmr16/tms/blob/master/Core/TMS.Core/Endpoints/Employee.cs"
    click Errors "https://github.com/premkmr16/tms/blob/master/Core/TMS.Core/Common/Errors/ErrorMessages.cs"

    %% Click Events - Infrastructure
    click Persistence "https://github.com/premkmr16/tms/blob/master/Infrastructure/TMS.Infrastructure/Persistence/Context/TMSDbContext.cs"
    click Persistence "https://github.com/premkmr16/tms/blob/master/Infrastructure/TMS.Infrastructure/Persistence/Repositories/EmployeeRepository/EmployeeReadRepository.cs"
    click ConnFactory "https://github.com/premkmr16/tms/blob/master/Infrastructure/TMS.Infrastructure/ConnectionFactory/ConnectionFactory.cs"
    click Hubs "https://github.com/premkmr16/tms/blob/master/Infrastructure/TMS.Infrastructure/Hubs/EmployeeHub.cs"

    %% Click Events - Database
    click StoredProcs "https://github.com/premkmr16/tms/blob/master/Database/StoredProcedures/GetEmployees.sql"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:2px
    classDef presentation fill:#cce5ff,stroke:#004085,stroke-width:2px
    classDef application fill:#d4edda,stroke:#155724,stroke-width:2px
    classDef core fill:#fff3cd,stroke:#856404,stroke-width:2px
    classDef infrastructure fill:#f8d7da,stroke:#721c24,stroke-width:2px
    classDef database fill:#e2e3e5,stroke:#6c757d,stroke-width:2px
```
