# bentran1vn/mbs-command architecture

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

- Interactive diagram: https://gitdiagram.com/bentran1vn/mbs-command
- Repository: https://github.com/bentran1vn/mbs-command

## Overview

##### Project and Its Purpose
- The project is part of a Mentor Booking System that serves three distinct user roles (Students, Mentors, Admins) through a clean, modular backend architecture.
- The README confirms features like CQRS, Repository, Outbox, and Idempotency patterns, indicating a structured approach to separating read (using MongoDB) and write (using MSSQL Server) operations.

##### File Structure
- The repository is organized into multiple modules/layers that reflect a Clean Architecture:
- MBS-COMMAND.API: Acts as the API entry point, configuring middleware (exception handling, JWT, Swagger) and hosting the ASP.NET Core application.
- MBS_COMMAND.Application: Contains abstractions, command handlers (CQRS pattern), validator and pipeline behaviors, and dependency injection setup for the application layer.
- MBS_COMMAND.Contract: Defines common contracts, messages, enumerations, and shared types for inter-module communication.
- MBS_COMMAND.Domain: Houses domain entities, aggregates, business rules, exceptions and interfaces that capture the core business logic.
- MBS_COMMAND.Infrastucture: Implements cross-cutting concerns like caching (Redis), authentication (JWT), mailing, media handling (Cloudinary) and background jobs, plus the integration with RabbitMQ/MassTransit.
- MBS_COMMAND.Persistence: Implements the database logic using EF Core, with configurations, migrations and the repository base pattern to interact with MSSQL Server (and possibly MongoDB for read operations).
- MBS_COMMAND.Presentation: Contains REST API-specific endpoint implementations and global configuration to expose functionalities to external clients.
- Supporting files for Docker, GitHub workflows, and launch settings complete the build/deployment automation and containerization.

##### Main Components and Their Relationships
- API Gateway/Entry-point: (MBS-COMMAND.API) responsible for handling incoming requests and dispatching them to appropriate endpoints.
- Application Layer: (MBS_COMMAND.Application) orchestrates command and query handling using the Mediator pattern (MediatR) and contains pipeline behaviors for logging, validation, performance measurement, etc.
- Domain Layer: (MBS_COMMAND.Domain) centralizes business logic and domain entities, ensuring the core rules and invariants.
- Infrastructure Layer: (MBS_COMMAND.Infrastucture) provides implementation details for external systems (caching with Redis, sending emails, handling media uploads) and integrates RabbitMQ/MassTransit for reliable messaging alongside implementing the Outbox pattern.
- Persistence Layer: (MBS_COMMAND.Persistence) manages EF Core based interactions including context, configurations, repository implementations, and database migrations for transactional (MSSQL) data persistence. It also supports converting domain events to outbox messages.
- Presentation Layer: (MBS_COMMAND.Presentation) exposes API endpoints for modules such as Groups, Feedbacks, Mentor Skills, Schedules, Slots, Subjects, and Users.

##### Architectural Patterns and Technologies to Highlight
- Clean Architecture: Emphasize the separation of concerns by isolating the domain from infrastructure and application concerns.
- Repository & Outbox Patterns: Illustrate the repository abstraction in the domain and persistence layers and mark the Outbox pattern for consistent event handling.
- Technology stack:
- Backend: ASP.NET Core 8, EF Core, MediatR, RabbitMQ/MassTransit.
- Databases: MSSQL Server (writes/transactions), MongoDB (read-heavy operations), and caching using Redis.
- Others: Docker for containerization, GitHub Actions for CI/CD, Nginx & YARP Reverse Proxy for routing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% API Gateway/Entry-point
    subgraph "API Gateway/Entry"
        API("MBS-COMMAND.API"):::service
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        PRES("MBS_COMMAND.Presentation<br/>(REST API Endpoints)"):::service
    end

    %% Application Layer (CQRS)
    subgraph "Application Layer"
        APP("MBS_COMMAND.Application<br/>(CQRS Handlers, Pipeline Behaviors)"):::service
    end

    %% Domain Layer
    subgraph "Domain Layer"
        DOM("MBS_COMMAND.Domain<br/>(Business Logic, Entities & Rules)"):::service
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        PER("MBS_COMMAND.Persistence<br/>(EF Core, Repositories, Outbox & Migrations)"):::service
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        INF("MBS_COMMAND.Infrastucture<br/>(Caching, Messaging, External Integrations)"):::service
    end

    %% Contract Layer (Shared Definitions)
    subgraph "Contract Layer"
        CON("MBS_COMMAND.Contract<br/>(Shared Contracts, Commands, Events)"):::contract
    end

    %% External Systems
    MSSQL("MSSQL Server<br/>(Write Database)"):::external
    MONGODB("MongoDB<br/>(Read Database)"):::external
    RABBIT("RabbitMQ/MassTransit<br/>(Messaging)"):::external
    REDIS("Redis Cache"):::external

    %% Data Flow
    API -->|"routes"| PRES
    PRES -->|"calls"| APP
    APP -->|"dispatches commands"| DOM
    DOM -->|"persists data"| PER
    PER -->|"writes to"| MSSQL
    APP -->|"queries from"| MONGODB
    DOM -->|"publishes events"| INF
    INF -->|"sends messages"| RABBIT
    INF -->|"caches data"| REDIS

    %% Contract usage links (dotted lines)
    APP ---|"uses"| CON
    DOM ---|"communicates via"| CON

    %% Styles
    classDef service fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef external fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef contract fill:#fff3cd,stroke:#856404,stroke-width:2px;

    %% Click Events
    click API "https://github.com/bentran1vn/mbs-command/blob/main/MBS-COMMAND.API"
    click PRES "https://github.com/bentran1vn/mbs-command/blob/main/MBS_COMMAND.Presentation"
    click APP "https://github.com/bentran1vn/mbs-command/blob/main/MBS_COMMAND.Application"
    click CON "https://github.com/bentran1vn/mbs-command/blob/main/MBS_COMMAND.Contract"
    click DOM "https://github.com/bentran1vn/mbs-command/blob/main/MBS_COMMAND.Domain"
    click INF "https://github.com/bentran1vn/mbs-command/blob/main/MBS_COMMAND.Infrastucture"
    click PER "https://github.com/bentran1vn/mbs-command/blob/main/MBS_COMMAND.Persistence"
```
