# pratiksinghlad/cqrspatternapi architecture

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

- Interactive diagram: https://gitdiagram.com/pratiksinghlad/cqrspatternapi
- Repository: https://github.com/pratiksinghlad/cqrspatternapi

## Overview

##### Project Type and Purpose
- Project Type: This is a CQRS (Command Query Responsibility Segregation) example built on .NET 8.
- Purpose: The application demonstrates how to separate command (write) operations from query (read) operations. It uses the Mediator pattern (via MediatR) and utilizes separate database contexts for write and read operations, following an Onion Layer architecture to enforce dependency inversion.

##### File Structure
- Main Components:
- CQRSPattern.Api: The web API hosting project. It contains controllers (like EmployeesController) and startup configurations (multiple Startup.*.cs files for health checks, mediator, scalar, etc.).
- CQRSPattern.Application: The core business logic layer with commands, queries, validators, and domain models (e.g., Employee commands for add, update, and queries for get all).
- CQRSPattern.Infrastructure.Persistence: Handles database-related concerns including separate DbContexts (WriteDbContext, ReadDbContext), entity configurations, migrations, and repository implementations both for read and write.
- CQRSPattern.Infrastructure.Mediator: Encapsulates MediatR configurations, mediator factories, and custom pipeline behaviors (e.g., validation behaviors).
- CQRSPattern.Migrator: A dedicated project for handling migrations and possibly setting up the database (with its own Program, HostedService, and connection factory).
- Test Projects (Application Test, Infrastructure Persistence Test, and Shared Test): They provide unit test coverage for the application and persistence layers.

##### Key Architectural Elements from the README
- CQRS Pattern: Emphasizes separation between command operations (modifying data, e.g., AddEmployee, UpdateEmployee) and query operations (reading data, e.g., GetAllQuery).
- Onion/Layers: The architecture respects dependency directions from controllers to the application core, and from inner layers to infrastructure.
- Mediator Pattern: The Application layer uses the MediatR library to decouple the controllers from business logic.
- Database Separation: Two distinct database contexts (read and write) are used, improving scalability and potential security.
- External Tools/Frameworks: Uses Entity Framework Core for ORM, OpenAPI for API documentation, and Scalar (an alternative/testing tool for Swagger) for API calling and testing.

a. Main Components to Include:
- API Layer (CQRSPattern.Api):
- Contains endpoints like the Employee Controller.
- Handles incoming HTTP requests, applies routing, and passes responsibilities via MediatR.
- Implements health checks and configuration settings.
- Application Layer (CQRSPattern.Application):
- Includes business logic with Commands (e.g., AddEmployeeCommand, UpdateEmployeeCommand) and Queries (e.g., GetAllQuery).
- Defines validators and command handlers.
- Acts as the entry point for MediatR which decouples direct calls between UI and business logic.
- Infrastructure Layer:
- Persistence: Contains separate DbContexts (ReadDbContext for queries, WriteDbContext for commands), entity configurations, repositories (IEmployeeReadRepository, IEmployeeWriteRepository), and migrations.
- Mediator: Contains the mediator implementations, custom behaviors (e.g., validation), and related utilities.
- External Integration: Any health checks, environment configuration files (appsettings.json variants), and database connection managers (e.g., MySqlConnectionManager).
- Migrator:
- A supporting service responsible for applying migrations and database setup.
b. Relationships and Interactions:
- API layer receives HTTP requests and routes them to the appropriate Controller actions.
- Controllers pass commands or queries to the Mediator (MediatR) in the Application layer.
- The Application layer processes requests via appropriate handlers:
- For commands (create, update), the flow goes to repository methods in the Infrastructure Persistence layer using WriteDbContext.
- For queries, it reaches the Infrastructure Persistence layer using ReadDbContext.
- The Mediator component ensures decoupled communication and centralized handling of requests.
- Configuration and startup files in the API layer integrate health checks, MediatR setup, and documentation (OpenAPI).

##### Architectural Patterns to Highlight
- CQRS Pattern: Separate flows for command (write) and query (read) operations.
- Onion/Layered Architecture: Strict dependency flow from the outer API layer to the inner Core/Application layer, then to Infrastructure.
- Mediator Pattern: Decoupling via MediatR, facilitating separation of concerns.
- Persistence Pattern: Using separate contexts to isolate read and write operations.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "API Layer"
        API["API (Hosting)"]:::api
        EmployeesController["EmployeesController"]:::api
        StartupConfig["Startup & Config"]:::api
        API --> EmployeesController
        API --> StartupConfig
    end

    API -->|"HTTP_Request"| AppMediator["App Mediator (MediatR)"]:::mediator

    subgraph "Application Layer"
        direction TB
        subgraph "Commands"
            AddCmd["AddEmployeeCommand"]:::app
            AddCmdHandler["AddEmployeeCommandHandler"]:::app
            AddCmdValidator["AddEmployeeCommandValidator"]:::app
            UpdateCmd["UpdateEmployeeCommand"]:::app
            UpdateCmdHandler["UpdateEmployeeCommandHandler"]:::app
            UpdateCmdValidator["UpdateEmployeeCommandValidator"]:::app
        end
        subgraph "Queries"
            GetAllQuery["GetAllQuery"]:::app
            GetAllQueryHandler["GetAllQueryHandler"]:::app
            GetAllQueryResult["GetAllQueryResult"]:::app
        end
        subgraph "Repository Interfaces"
            IWriteRepo["IEmployeeWriteRepository"]:::app
            IReadRepo["IEmployeeReadRepository"]:::app
        end
    end

    AppMediator -->|"commands"| AddCmdHandler
    AppMediator -->|"queries"| GetAllQueryHandler

    AddCmd --> AddCmdHandler
    UpdateCmd --> UpdateCmdHandler
    GetAllQuery --> GetAllQueryHandler

    AddCmdHandler --> IWriteRepo
    UpdateCmdHandler --> IWriteRepo
    GetAllQueryHandler --> IReadRepo

    subgraph "Infrastructure Layer"
        direction TB
        subgraph "Persistence"
            EmployeeWriteRepo["EmployeeWriteRepository"]:::infra
            EmployeeReadRepo["EmployeeReadRepository"]:::infra
            WriteDB["Write Database (WriteDbContext)"]:::infra
            ReadDB["Read Database (ReadDbContext)"]:::infra
        end
        subgraph "Infrastructure Mediator"
            InfraMediator["Infra Mediator (MediatR)"]:::mediator
        end
    end

    IWriteRepo --> EmployeeWriteRepo
    EmployeeWriteRepo --> WriteDB
    IReadRepo --> EmployeeReadRepo
    EmployeeReadRepo --> ReadDB

    AppMediator --> InfraMediator

    subgraph "Migrator"
        Migrator["Migrator"]:::migrator
    end

    Migrator --- WriteDB
    Migrator --- ReadDB

    %% Click Events
    click API "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Api"
    click EmployeesController "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Api/Features/Employee/EmployeesController.cs"
    click StartupConfig "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Api/Startup.cs"
    click AppMediator "https://github.com/pratiksinghlad/cqrspatternapi/tree/main/CQRSPattern.Application/Mediator"
    click AddCmd "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Application/Features/Employee/Add/AddEmployeeCommand.cs"
    click AddCmdHandler "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Application/Features/Employee/Add/AddEmployeeCommandHandler.cs"
    click AddCmdValidator "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Application/Features/Employee/Add/AddEmployeeCommandValidator.cs"
    click UpdateCmd "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Application/Features/Employee/Update/UpdateEmployeeCommand.cs"
    click UpdateCmdHandler "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Application/Features/Employee/Update/UpdateEmployeeCommandHandler.cs"
    click UpdateCmdValidator "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Application/Features/Employee/Update/UpdateEmployeeCommandValidator.cs"
    click GetAllQuery "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Application/Features/Employee/GetAll/GetAllQuery.cs"
    click GetAllQueryHandler "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Application/Features/Employee/GetAll/GetAllQueryHandler.cs"
    click GetAllQueryResult "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Application/Features/Employee/GetAll/GetAllQueryResult.cs"
    click IWriteRepo "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Application/Repositories/Write/IEmployeeWriteRepository.cs"
    click IReadRepo "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Application/Repositories/Read/IEmployeeReadRepository.cs"
    click EmployeeWriteRepo "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Infrastructure.Persistence/Repositories/Write/EmployeeWriteRepository.cs"
    click EmployeeReadRepo "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Infrastructure.Persistence/Repositories/Read/EmployeeReadRepository.cs"
    click WriteDB "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Infrastructure.Persistence/Database/WriteDbContext.cs"
    click ReadDB "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Infrastructure.Persistence/Database/ReadDbContext.cs"
    click InfraMediator "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Infrastructure.Mediator"
    click Migrator "https://github.com/pratiksinghlad/cqrspatternapi/blob/main/CQRSPattern.Migrator"

    %% Styles
    classDef api fill:#ffcccc,stroke:#b30000,stroke-width:2px;
    classDef app fill:#ccffcc,stroke:#006600,stroke-width:2px;
    classDef infra fill:#cce5ff,stroke:#004d99,stroke-width:2px;
    classDef mediator fill:#ffebcc,stroke:#e69500,stroke-width:2px;
    classDef migrator fill:#ffffcc,stroke:#999900,stroke-width:2px;
```
