# gabriel7729/quickchatapi architecture

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

- Interactive diagram: https://gitdiagram.com/gabriel7729/quickchatapi
- Repository: https://github.com/gabriel7729/quickchatapi

## Overview

##### Main Components and Layers
- API/Web Layer – The "ApiTemplate.Web" project hosts the endpoints, configuration, and middleware. This layer is responsible for handling HTTP requests, user authentication, endpoint routing, and managing client interactions.
- Core/Domain Layer – The "ApiTemplate.Core" project contains business logic, domain entities (such as Chat, User, and Validation aggregates), abstractions, specifications, interfaces, constants, and core services. This layer encapsulates the core business rules.
- Infrastructure/Data Layer – The "ApiTemplate.Infrastructure" project handles data persistence and external integrations, including the Entity Framework context (AppDbContext), repository implementations, migrations, DTO mappings, and third-party integrations like email and PDF services.
- Shared Kernel – The "ApiTemplate.SharedKernel" provides commonly used abstractions, base classes (like EntityBase), domain event handling, and specifications that are used across other layers.

##### Relationships and Interactions
- The Web layer communicates with the Core layer by invoking business logic (for example, through endpoints such as authentication, chat, and user management endpoints) while applying concepts like dependency injection (via Autofac).
- The Core layer, which defines interfaces (e.g., IAuthService, IChatService, IOtpService, etc.), delegates persistence operations to the Infrastructure layer.
- The Infrastructure layer implements these interfaces and handles data access through the Entity Framework repository methods.
- Domain events (using Mediator pattern) and outbox patterns are used for asynchronous event emission and handling, ensuring robust decoupled interactions between services.
- Specifications (as described in the Ardalis Specifications approach) are used for dynamically passing query requirements from the Core to Infrastructure layer, encapsulating query logic.

##### Architectural Patterns & Design Principles
- Clean Architecture / Onion Architecture – Separation into distinct layers (UI/Web, Core/Domain, Infrastructure, and Shared Kernel) minimizes coupling.
- Dependency Injection and Inversion of Control (using Autofac) – Dependencies flow inward, with each layer only aware of abstractions defined by inner layers.
- Use of Specifications – For building database queries in a clean and reusable manner.
- Guard Clauses – Used to enforce preconditions in business logic.
- Event-driven communication – Using Mediator to dispatch domain events.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Client
    Client["Client/External Consumer"]:::client

    %% API/Web Layer
    subgraph "API/Web Layer"
        API_Config["Configuration & Middleware"]:::web
        Auth["Authentication Endpoints"]:::web
        Chat["Chat Endpoints"]:::web
        User["User Endpoints"]:::web
        Valid["Validations Endpoints"]:::web
        Health["HealthCheck Endpoints"]:::web
    end

    %% Core/Domain Layer
    subgraph "Core/Domain Layer"
        Core_Entities["Domain Entities"]:::core
        Core_Interfaces["Abstractions & Interfaces"]:::core
        Core_Services["Domain Services"]:::core
        Core_Specs["Specifications"]:::core
        Core_Module["DefaultCoreModule"]:::core
    end

    %% Infrastructure/Data Layer
    subgraph "Infrastructure/Data Layer"
        Infra_DB["Database Context & Repositories"]:::infra
        Infra_Migrations["Migrations"]:::infra
        Infra_Mapper["DTO Mappings & Profiles"]:::infra
        Infra_Services["Infrastructure Services"]:::infra
        Infra_Module["DefaultInfrastructureModule"]:::infra
    end

    %% Shared Kernel
    subgraph "Shared Kernel"
        Shared_Events["Domain Events & Dispatcher"]:::shared
        Shared_Base["Common Base Entities & Specs"]:::shared
    end

    %% Connections between layers
    Client -->|"sends request"| API_Config
    API_Config -->|"routes to"| Auth
    API_Config -->|"routes to"| Chat
    API_Config -->|"routes to"| User
    API_Config -->|"routes to"| Valid
    API_Config -->|"routes to"| Health

    Auth -->|"calls"| Core_Services
    Chat -->|"calls"| Core_Services
    User -->|"calls"| Core_Services
    Valid -->|"calls"| Core_Services
    Health -->|"calls"| Core_Services

    Core_Services -->|"persists data"| Infra_DB
    Core_Services -->|"integrates with"| Infra_Services
    Core_Services -->|"fires events"| Shared_Events

    %% Internal Core dependencies (dashed for internal references)
    Core_Services --- Core_Entities
    Core_Services --- Core_Interfaces
    Core_Services --- Core_Specs
    Core_Interfaces --- Core_Module

    %% Internal Infrastructure dependencies
    Infra_DB --- Infra_Migrations
    Infra_Mapper --- Infra_Services
    Infra_Services --- Infra_Module

    %% Styles for layers
    classDef client fill:#e0bbf4,stroke:#5e0eac,stroke-width:2px;
    classDef web fill:#bbd1fa,stroke:#1d65a6,stroke-width:2px;
    classDef core fill:#baffc9,stroke:#2d8f4a,stroke-width:2px;
    classDef infra fill:#ffdd99,stroke:#d48806,stroke-width:2px;
    classDef shared fill:#d3d3d3,stroke:#808080,stroke-width:2px;

    %% Click Events for API/Web Layer
    click Auth "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Web/Endpoints/AuthEndpoints"
    click Chat "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Web/Endpoints/ChatEndpoints"
    click User "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Web/Endpoints/UserEndpoints"
    click Valid "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Web/Endpoints/ValidationsEndpoints"
    click Health "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Web/Endpoints/HealthCheckEndpoints"
    click API_Config "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Web/Config"

    %% Click Events for Core/Domain Layer
    click Core_Entities "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Core/Entities"
    click Core_Interfaces "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Core/Abstracts"
    click Core_Services "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Core/Services"
    click Core_Specs "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Core/Entities/ChatAggregate/Specifications"
    click Core_Module "https://github.com/gabriel7729/quickchatapi/blob/main/src/ApiTemplate.Core/DefaultCoreModule.cs"

    %% Click Events for Infrastructure/Data Layer
    click Infra_DB "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Infrastructure/Data"
    click Infra_Migrations "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Infrastructure/Migrations"
    click Infra_Mapper "https://github.com/gabriel7729/quickchatapi/blob/main/src/ApiTemplate.Infrastructure/Config/MainMapperProfile.cs"
    click Infra_Services "https://github.com/gabriel7729/quickchatapi/tree/main/src/ApiTemplate.Infrastructure/Services"
    click Infra_Module "https://github.com/gabriel7729/quickchatapi/blob/main/src/ApiTemplate.Infrastructure/DefaultInfrastructureModule.cs"

    %% Click Events for Shared Kernel
    click Shared_Events "https://github.com/gabriel7729/quickchatapi/blob/main/src/ApiTemplate.SharedKernel/DomainEventBase.cs"
    click Shared_Base "https://github.com/gabriel7729/quickchatapi/blob/main/src/ApiTemplate.SharedKernel/EntityBase.cs"
```
