# montarelab/microservices-cqrs-eventsource architecture

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

- Interactive diagram: https://gitdiagram.com/montarelab/microservices-cqrs-eventsource
- Repository: https://github.com/montarelab/microservices-cqrs-eventsource

## Overview

##### Main Components
- Command Side (Write Operations):
- Command API (located in the Post.Cmd project) which receives commands such as NewPostCommand, LikePostCommand, etc.
- Command Handlers that implement business logic.
- Aggregate Root and domain logic that ensure consistency.
- Event Sourcing mechanisms that write state changes as events.
- Event Store implemented with MongoDB.
- Event Producers that publish events to Kafka.

- Query Side (Read Operations):
- Query API (from the Post.Query project) which exposes endpoints for fetching data.
- Query Handlers that process incoming queries and fetch data from the read database.
- A denormalized Read Model stored in MSSQL.
- Event Consumers that pick up events from Kafka and update the read model.

- Messaging and Integration:
- Kafka used as the central message bus for sending and consuming domain events.
- Data flows between the Command and Query sides via published events, ensuring eventual consistency.

##### File Structure
- The top-level structure under the “src” directory clearly distinguishes the core libraries (CQRS.Core) from the service-specific projects (SM-POST which includes Post.Cmd, Post.Query, Post.Common, etc.).
- Directories such as “Commands”, “Events”, “Consumers”, “Handlers”, and “Producers” indicate a strong pursuit of the CQRS pattern with event-driven behavior.
- Configuration files (e.g., appsettings.json for both Command and Query APIs, docker-compose.yml) emphasize deployment and orchestration using Docker Compose.
- The presence of infrastructure directories indicates explicit roles (e.g., command dispatchers, event sourcing handlers).

##### Detail the Relationships and Interactions
- A client sends a command to the Command API. The command is processed by the command handler which enforces business logic.
- After processing, the event reflecting the state change is stored in the MongoDB-based Event Store.
- The event is simultaneously published onto Kafka.
- Kafka then forwards these events to the Query side where consumers pick them up.
- Query Handlers update the MSSQL-based Read Database accordingly.
- When a read request (query) comes in, the Query API fetches information from the updated, denormalized database and returns it to the client.

##### Architectural Patterns & Design Principles
- CQRS: Splitting the system into separate models for write and read operations.
- Event Sourcing: Capturing all changes as immutable events.
- Microservices: Distinct projects/microservices (command and query) communicating via Kafka.
- Dependency Injection & MediatR usage to dispatch commands and queries.
- Clear separation of concerns across layers (domain, infrastructure, API).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    Client("Client"):::external

    %% Command Side
    subgraph "Command Side"
        cmdAPI("Command API"):::api
        cmdHandlers("Command Handlers"):::handler
        domain("Domain & Aggregate Root"):::domain
        eventStore("MongoDB Event Store"):::database
        eventProducers("Event Producers"):::messaging
    end

    %% Messaging (Kafka)
    kafka("Kafka Message Bus"):::messaging

    %% Query Side
    subgraph "Query Side"
        queryAPI("Query API"):::api
        queryHandlers("Query Handlers & Dispatcher"):::handler
        eventConsumers("Event Consumers"):::messaging
        readDB("Read Database (MSSQL)"):::database
    end

    %% Shared Core
    cqrsCore("CQRS Core"):::external

    %% Connections - Command Flow
    Client -->|"HTTP"| cmdAPI
    cmdAPI -->|"dispatches"| cmdHandlers
    cmdHandlers -->|"applies_logic"| domain
    cmdHandlers -->|"stores_event"| eventStore
    cmdHandlers -->|"publishes_event"| eventProducers
    eventStore -->|"logs_event"| kafka
    eventProducers -->|"publishes_to"| kafka

    %% Connections - Query Flow
    kafka -->|"consumes_event"| eventConsumers
    eventConsumers -->|"updates_model"| readDB
    queryAPI -->|"processes_query"| queryHandlers
    queryHandlers -->|"fetches_data"| readDB
    Client -->|"HTTP_Query"| queryAPI
    queryAPI -->|"returns_data"| Client

    %% Shared Core Usage
    cmdAPI ---|"uses"| cqrsCore
    queryAPI ---|"uses"| cqrsCore

    %% Click Events
    click cmdAPI "https://github.com/montarelab/microservices-cqrs-eventsource/blob/main/src/SM-POST/Post.Cmd/Post.Cmd.Api"
    click cmdHandlers "https://github.com/montarelab/microservices-cqrs-eventsource/tree/main/src/SM-POST/Post.Cmd/Post.Cmd.Api/Commands"
    click domain "https://github.com/montarelab/microservices-cqrs-eventsource/blob/main/src/SM-POST/Post.Cmd/Post.Cmd.Domain/Aggregates/PostAggregate.cs"
    click eventStore "https://github.com/montarelab/microservices-cqrs-eventsource/blob/main/src/SM-POST/Post.Cmd/Post.Cmd.Infrastructure/Stores/EventStore.cs"
    click eventProducers "https://github.com/montarelab/microservices-cqrs-eventsource/blob/main/src/SM-POST/Post.Cmd/Post.Cmd.Infrastructure/Producers/EventProducer.cs"
    click queryAPI "https://github.com/montarelab/microservices-cqrs-eventsource/blob/main/src/SM-POST/Post.Query/Post.Query.Api"
    click queryHandlers "https://github.com/montarelab/microservices-cqrs-eventsource/tree/main/src/SM-POST/Post.Query/Post.Query.Api/Queries"
    click readDB "https://github.com/montarelab/microservices-cqrs-eventsource/blob/main/src/SM-POST/Post.Query/Post.Query.Infrastructure/DataAccess/DatabaseContext.cs"
    click eventConsumers "https://github.com/montarelab/microservices-cqrs-eventsource/blob/main/src/SM-POST/Post.Query/Post.Query.Infrastructure/Consumers/EventConsumer.cs"
    click cqrsCore "https://github.com/montarelab/microservices-cqrs-eventsource/blob/main/src/CQRS.Core"

    %% Styles
    classDef api fill:#DDEEFF,stroke:#2F7FBF,stroke-width:2px;
    classDef handler fill:#FFEFD5,stroke:#D2691E,stroke-width:2px;
    classDef domain fill:#E6E6FA,stroke:#483D8B,stroke-width:2px;
    classDef database fill:#F0FFF0,stroke:#2E8B57,stroke-width:2px;
    classDef messaging fill:#FFFACD,stroke:#DAA520,stroke-width:2px;
    classDef external fill:#FFCCCC,stroke:#CC0000,stroke-width:2px;
```
