# madslundt/netcoremicroservicessample architecture

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

- Interactive diagram: https://gitdiagram.com/madslundt/netcoremicroservicessample
- Repository: https://github.com/madslundt/netcoremicroservicessample

## Overview

##### Main Components
- API Gateway: Represent the public entry point using Ocelot that routes client HTTP requests to the right microservice.
- Microservices: Create separate components for MoviesService, ReviewsService, and UsersService. Each microservice should be pictured as an autonomous unit hosting its own REST API, and handling its own commands, queries, controllers, event handlers, and even update handlers (when using event sourcing).
- Event Publishing/Subscription Elements: Show how microservices publish domain events (for instance, via a command bus and event bus) and subscribe to events using a message broker.
- Infrastructure Services: Illustrate the shared Infrastructure layer that provides:
- Consul (for service discovery)
- Core functionalities (commands, queries, events, validation, mapping, exception filtering)
- EventStore (supporting MongoDB and EF Core options)
- Outbox pattern (for reliable event publishing)
- MessageBrokers (with RabbitMQ as the primary broker)
- Logging (through Serilog and ELK/Kibana)
- Swagger (for API documentation)
- External Dependencies: Include external components from the Compose folder such as SQL Server, RabbitMQ, Elasticsearch, Kibana, and any other containerized services brought in via Docker Compose.

##### Relationships and Interaction Flows
- Components from the Infrastructure layer to the microservices:

##### Architectural Patterns and Key Technologies
- Use of Event Sourcing and Outbox patterns (especially in MoviesService and ReviewsService) to capture and reliably publish domain events.
- That communication between services is event-driven (via RabbitMQ) while REST API calls are used for direct client and inter-service requests routed through the API Gateway.
- Inclusion of Consul for dynamic service discovery, meaning that microservices locate one another by name or ID rather than hardcoded URIs.
- Text labels for key third-party tools such as:
- Ocelot (for API Gateway implementation)
- MediatR (for internal mediator pattern implementation in Commands, Queries, and Event handling)
- EF Core & MongoDB (for persistence in the EventStore and Outbox)

##### Putting It All Together
- Then detail each microservice’s internal architecture and the supporting Infrastructure layer.
- Annotations for service discovery (Consul), event persistence (EventStore), and Outbox processing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["Clients"]:::client

    %% API Gateway
    APIGateway["API Gateway (Ocelot)"]:::gateway

    %% Microservices Subgraph
    subgraph "Microservices"
        direction TB
        Movies["MoviesService\n(REST API, CQRS, Event Sourcing)"]:::micro
        Reviews["ReviewsService\n(REST API, CQRS, Outbox)"]:::micro
        Users["UsersService\n(REST API, CQRS)"]:::micro
    end

    %% Infrastructure Services Subgraph
    subgraph "Infrastructure Services"
        direction TB
        CQRS["Shared CQRS Infrastructure\n(MediatR, Validation)"]:::infra
        EventStore["Event Store\n(EF Core/MongoDB)"]:::infra
        Outbox["Outbox Pattern\n(Reliable Event Publishing)"]:::infra
        MessageBroker["Message Broker\n(RabbitMQ)"]:::infra
        Consul["Service Discovery\n(Consul)"]:::infra
        Logging["Logging\n(Serilog, ELK/Kibana)"]:::infra
        Swagger["API Documentation\n(Swagger)"]:::infra
    end

    %% External Dependencies / Orchestration Subgraph
    subgraph "External Dependencies / Orchestration"
        direction TB
        SQL["SQL Server"]:::external
        ERabbit["RabbitMQ"]:::external
        Elasticsearch["Elasticsearch"]:::external
        Kibana["Kibana"]:::external
        Docker["Docker Compose"]:::external
    end

    %% Connections
    Client -->|"HTTP_Request/Response"| APIGateway
    APIGateway -->|"Routes_to"| Movies
    APIGateway -->|"Routes_to"| Reviews
    APIGateway -->|"Routes_to"| Users

    %% Microservices interactions with Message Broker (Event Publishing/Subscription)
    Movies -->|"Publishes/Consumes_events"| MessageBroker
    Reviews -->|"Publishes/Consumes_events"| MessageBroker
    Users -->|"Publishes/Consumes_events"| MessageBroker

    %% Persistence & Outbox
    Movies -->|"Stores_events"| EventStore
    Reviews -->|"Stores_events"| EventStore
    Movies -->|"Uses_outbox"| Outbox
    Reviews -->|"Uses_outbox"| Outbox

    %% Service Discovery
    Movies -->|"Service_discovery"| Consul
    Reviews -->|"Service_discovery"| Consul
    Users -->|"Service_discovery"| Consul

    %% Logging
    Movies -->|"Logs_activity"| Logging
    Reviews -->|"Logs_activity"| Logging
    Users -->|"Logs_activity"| Logging

    %% Shared CQRS usage
    Movies -->|"CQRS_commands/queries"| CQRS
    Reviews -->|"CQRS_commands/queries"| CQRS
    Users -->|"CQRS_commands/queries"| CQRS

    %% API Gateway API Documentation connection
    APIGateway -->|"API_documentation"| Swagger

    %% Infrastructure to External Dependencies
    MessageBroker -->|"Communicates_with"| ERabbit
    Logging -->|"Sends_logs_to"| Elasticsearch
    Elasticsearch -->|"Visualization"| Kibana

    %% External orchestration connections
    SQL --- Docker
    ERabbit --- Docker
    Elasticsearch --- Docker
    Kibana --- Docker

    %% Click Events
    click APIGateway "https://github.com/madslundt/netcoremicroservicessample/tree/master/ApiGateway/Ocelot"
    click Movies "https://github.com/madslundt/netcoremicroservicessample/tree/master/Src/MoviesService"
    click Reviews "https://github.com/madslundt/netcoremicroservicessample/tree/master/Src/ReviewsService"
    click Users "https://github.com/madslundt/netcoremicroservicessample/tree/master/Src/UsersService"
    click CQRS "https://github.com/madslundt/netcoremicroservicessample/tree/master/Infrastructure/Core"
    click EventStore "https://github.com/madslundt/netcoremicroservicessample/tree/master/Infrastructure/EventStores"
    click Outbox "https://github.com/madslundt/netcoremicroservicessample/tree/master/Infrastructure/Outbox"
    click MessageBroker "https://github.com/madslundt/netcoremicroservicessample/tree/master/Infrastructure/MessageBrokers"
    click Consul "https://github.com/madslundt/netcoremicroservicessample/tree/master/Infrastructure/Consul"
    click Logging "https://github.com/madslundt/netcoremicroservicessample/tree/master/Infrastructure/Logging"
    click Swagger "https://github.com/madslundt/netcoremicroservicessample/tree/master/Infrastructure/Swagger"
    click Docker "https://github.com/madslundt/netcoremicroservicessample/tree/master/Compose"

    %% Styles
    classDef client fill:#f4a261,stroke:#000,stroke-width:2px;
    classDef gateway fill:#2a9d8f,stroke:#000,stroke-width:2px;
    classDef micro fill:#e9c46a,stroke:#000,stroke-width:2px;
    classDef infra fill:#264653,stroke:#fff,stroke-width:2px;
    classDef external fill:#e76f51,stroke:#000,stroke-width:2px;
```
