# eriksena16/simplecqrsapiwithmediatr architecture

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

- Interactive diagram: https://gitdiagram.com/eriksena16/simplecqrsapiwithmediatr
- Repository: https://github.com/eriksena16/simplecqrsapiwithmediatr

## Overview

##### Project type and purpose
- This is a **backend API service** built with ASP.NET Core.
- It demonstrates **Domain-Driven Design (DDD)** layered architecture combined with **CQRS** and **MediatR** for message dispatching.

##### File structure
- Top-level solution: `SimpleCQRSApi.sln`
- Four main folders under `src/`:
1. **Simple.API**
– Controllers (`SalesController.cs`)
– Configuration (`appsettings.json`, `launchSettings.json`)
– HTTP client file (`Simple.API.http`)
2. **Simple.Application**
– Commands & Queries (`CreateSaleCommand.cs`, etc.)
– DTOs (Request/Response)
– Handlers (`CreateSaleCommandHandler`, …)
3. **Simple.Domain**
– Entities (`Sale`, `SaleItem`)
– Interfaces (Repositories, Services)
4. **Simple.Infrastructure**
– EF Core DbContext (`AppDbContext`)
– Repository implementations
– Domain service implementations (`OrderProcessor`)

##### README
- Confirms the four‐layer DDD approach: **API, Application, Domain, Infrastructure**
- Mentions use of **MediatR** for decoupling, and **CQRS** to separate reads and writes.

##### Components and interactions
- **API Layer** (ASP.NET Core Web API)
- **MediatR** (in-process message bus)
- **Application Layer** (Commands, Queries, Handlers)
- **Domain Layer** (Entities, Interfaces)
- **Infrastructure Layer** (EF Core DbContext, Repositories, Services)
- **Database** (e.g., SQL Server)
1. Client → HTTP Request → **API Controller**
2. Controller → sends **Command/Query** to **MediatR**
3. MediatR → dispatches to appropriate **Handler** in Application
4. Handler → invokes **Domain Entities** and **Domain Services** via interfaces
5. Handler → calls **Repository interface** → implemented by **Infrastructure** → interacts with **DbContext**
6. DbContext → reads/writes **Database**
7. Handler returns DTO → MediatR → Controller → HTTP Response → Client
c. Architectural patterns & principles:
- **Layered Architecture** (separate folders per layer)
- **CQRS** (Commands vs. Queries)
- **MediatR** for decoupled in‐process messaging
- **Dependency Inversion** (Domain interfaces → Infrastructure implementations)
- **Repository Pattern** via EF Core
d. Key technologies & frameworks:
- ASP.NET Core Web API
- MediatR
- Entity Framework Core
- SQL Server (or any relational DB)
- .NET 6+

##### Detail level
- Four projects as separate blocks.
- Within each block, optionally note subcomponents (Controllers, Handlers, Entities).

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client"]:::external

    subgraph "API Layer"
        direction TB
        Program["Program.cs"]:::api
        Controller["SalesController"]:::api
    end

    subgraph "MediatR (In-Process Bus)"
        direction TB
        MediatR["MediatR"]:::app
    end

    subgraph "Application Layer"
        direction TB
        CreateSaleCmd["CreateSaleCommand"]:::app
        ProcessOrderCmd["ProcessOrderCommand"]:::app
        GetListSaleQry["GetListSaleQuery"]:::app
        GetSaleByIdQry["GetSaleByIdQuery"]:::app
        CreateSaleHandler["CreateSaleCommandHandler"]:::app
        ProcessOrderHandler["ProcessOrderCommandHandler"]:::app
        GetListSaleHandler["GetListSaleQueryHandler"]:::app
        GetSaleByIdHandler["GetSaleByIdQueryHandler"]:::app
    end

    subgraph "Domain Layer"
        direction TB
        Entities["Entities (Sale, SaleItem)"]:::domain
        Interfaces["Interfaces (Repos, Services)"]:::domain
    end

    subgraph "Infrastructure Layer"
        direction TB
        DbContext["AppDbContext"]:::infra
        SaleRepo["SaleRepository"]:::infra
        OrderProcessorImpl["OrderProcessor"]:::infra
    end

    Database[(Database)]:::infra

    Client -->|"HTTP Request"| Controller
    Controller -->|"sends Command/Query"| MediatR
    MediatR -->|"dispatch CreateSale"| CreateSaleHandler
    MediatR -->|"dispatch ProcessOrder"| ProcessOrderHandler
    MediatR -->|"dispatch GetListSale"| GetListSaleHandler
    MediatR -->|"dispatch GetSaleById"| GetSaleByIdHandler

    CreateSaleHandler -->|"uses"| Entities
    ProcessOrderHandler -->|"uses"| Entities
    GetListSaleHandler -->|"uses"| Entities
    GetSaleByIdHandler -->|"uses"| Entities

    CreateSaleHandler -->|"calls Repo Interface"| Interfaces
    ProcessOrderHandler -->|"calls Repo Interface"| Interfaces
    GetListSaleHandler -->|"calls Repo Interface"| Interfaces
    GetSaleByIdHandler -->|"calls Repo Interface"| Interfaces

    Interfaces -->|"implemented by"| SaleRepo
    Interfaces -->|"implemented by"| OrderProcessorImpl

    SaleRepo -->|"uses DbContext"| DbContext
    OrderProcessorImpl -->|"uses DbContext"| DbContext
    DbContext -->|"reads/writes"| Database

    MediatR -->|"returns DTO"| Controller
    Controller -->|"HTTP Response"| Client

    click Program "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.API/Program.cs"
    click Controller "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.API/Controllers/SalesController.cs"
    click CreateSaleCmd "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.Application/Commands/CreateSaleCommand.cs"
    click ProcessOrderCmd "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.Application/Commands/ProcessOrderCommand.cs"
    click GetListSaleQry "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.Application/Queries/GetListSaleQuery.cs"
    click GetSaleByIdQry "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.Application/Queries/GetSaleByIdQuery.cs"
    click CreateSaleHandler "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.Application/Handlers/CreateSaleCommandHandler.cs"
    click ProcessOrderHandler "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.Application/Handlers/ProcessOrderCommandHandler.cs"
    click GetListSaleHandler "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.Application/Handlers/GetListSaleQueryHandler.cs"
    click GetSaleByIdHandler "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.Application/Handlers/GetSaleByIdQueryHandler.cs"
    click DbContext "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.Infrastructure/Context/AppDbContext.cs"
    click SaleRepo "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.Infrastructure/Repositories/SaleRepository.cs"
    click OrderProcessorImpl "https://github.com/eriksena16/simplecqrsapiwithmediatr/blob/master/src/Simple.Infrastructure/Services/OrderProcessor.cs"

    classDef api fill:#cce5ff,stroke:#000,stroke-width:1px
    classDef app fill:#d4edda,stroke:#000,stroke-width:1px
    classDef domain fill:#fff3cd,stroke:#000,stroke-width:1px
    classDef infra fill:#ffe5b4,stroke:#000,stroke-width:1px
    classDef external fill:#f8d7da,stroke:#000,stroke-width:1px
```
