# marethy/e-commerce_microservice_aspnetcore architecture

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

- Interactive diagram: https://gitdiagram.com/marethy/e-commerce_microservice_aspnetcore
- Repository: https://github.com/marethy/e-commerce_microservice_aspnetcore

## Overview

##### Project type and purpose
- This is a full-blown ASP.NET Core microservices-based e-commerce system.
- Purpose: independently deployable services (Product, Customer, Basket, Inventory, Ordering, Hangfire jobs, API Gateway, WebHealthStatus) that communicate via REST, gRPC and message bus to fulfill typical e-commerce flows (catalog browsing, order checkout, inventory updates).

##### File structure
- Top-level “src” splits into three areas:
- ApiGateways (Ocelot)
- BuildingBlocks (shared contracts, infrastructure, event bus, logging, common configs, DTOs)
- Services (each microservice: Basket.API, Customer.API, Product.API, Ordering (API + Application + Domain + Infrastructure), Inventory (Product API + gRPC), Hangfire.API)
- WebApps (WebHealthStatus UI)
- Each service has Controllers, Entities, Persistence or Repositories, Extensions (startup wiring), mapping profiles, Dockerfile and appsettings.
- Shared BuildingBlocks include:
- Contracts (interfaces, event definitions, repository interfaces)
- Infrastructure (Mongo/Redis/EF Core repos, RabbitMQ producer, SMTP email)
- Shared DTOs and settings.
- docker-compose.yml and override define containers: Postgres, MySQL, SQL Server, Redis, MongoDB, RabbitMQ, Elasticsearch, Kibana, Portainer, plus each service.

##### Extract additional insights from README
- Microservices communicate via:
- REST endpoints (ProductAPI, CustomerAPI, BasketAPI, OrderingAPI, Inventory.Product.API)
- gRPC (Internal Inventory.Grpc, Basket.API calls Inventory via gRPC.)
- RabbitMQ message bus for asynchronous events (BasketCheckout triggers Ordering via integration event).
- Databases per service:
- Product = MySQL, Customer = PostgreSQL, Ordering = SQL Server, Basket = Redis, Inventory = MongoDB.
- Infrastructure orchestration with Docker Compose.

a. API Gateway (Ocelot)
b. Client-facing layer: WebHealthStatus UI (MVC), external client calls to API Gateway
- Product.API (MySQL)
- Customer.API (PostgreSQL)
- Basket.API (Redis, gRPC client to Inventory.Grpc)
- Inventory.Grpc (MongoDB)
- Inventory.Product.API (MongoDB read/write)
- Ordering.API (SQL Server) + Ordering.Application + Ordering.Infrastructure + Consumers (RabbitMQ)
- Hangfire.API (background jobs)
d. Message Broker: RabbitMQ
e. External infra: Elasticsearch + Kibana, Portainer, PgAdmin

##### Relationships and interactions
- Client → API Gateway → appropriate REST service
- Basket.API → Inventory.Grpc via gRPC for stock validation
- Basket checkout → publish BasketCheckoutEvent → RabbitMQ → Ordering.API consumer
- Ordering.API → writes to SQL Server
- Each service reads/writes its own database
- Services use shared BuildingBlocks libraries for logging, repository patterns, event bus abstractions.

##### Architectural patterns and design principles
- Microservices per bounded context (DDD in Ordering.Domain)
- Clean layering in Ordering: API → Application (CQRS + MediatR behaviors: validation, performance, exception) → Domain → Infrastructure → Persistence
- API Gateway for façade routing and cross-cutting (authentication, throttling)
- Shared libraries for cross-cutting concerns (Contracts, Logging, EventBus)
- Asynchronous messaging for eventual consistency (RabbitMQ events)
- Containerized deployment with Docker Compose

##### Detail
- Microservice, its primary endpoints, its database, and its integration points
- Don’t omit the API Gateway or Docker Compose-defined infrastructure (Elasticsearch, Kibana, Portainer)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Legend: solid = REST/gRPC, dashed = async events

    %% External Client and UI
    subgraph "Client Layer"
        direction TB
        Client["External Client"]:::external
        WebHealth["WebHealthStatus UI"]:::frontend
    end

    Client -->|"HTTP"| API_Gateway
    WebHealth -->|"HTTP"| API_Gateway

    %% API Gateway
    API_Gateway["API Gateway (Ocelot)"]:::gateway
    click API_Gateway "https://github.com/marethy/e-commerce_microservice_aspnetcore/tree/main/src/ApiGateways/OcelotApiGw"

    %% Microservices
    subgraph "Microservices" 
        direction TB

        ProductAPI["Product.API<br/>(MySQL)"]:::api
        click ProductAPI "https://github.com/marethy/e-commerce_microservice_aspnetcore/blob/main/src/Services/Product.API"

        CustomerAPI["Customer.API<br/>(PostgreSQL)"]:::api
        click CustomerAPI "https://github.com/marethy/e-commerce_microservice_aspnetcore/blob/main/src/Services/Customer.API"

        BasketAPI["Basket.API<br/>(Redis + gRPC)"]:::api
        click BasketAPI "https://github.com/marethy/e-commerce_microservice_aspnetcore/blob/main/src/Services/Basket.API"

        subgraph "Inventory Services"
            direction TB
            InvGrpc["Inventory.Grpc<br/>(MongoDB)"]:::api
            click InvGrpc "https://github.com/marethy/e-commerce_microservice_aspnetcore/blob/main/src/Services/Inventory/Inventory.Grpc"
            InvProductAPI["Inventory.Product.API<br/>(MongoDB)"]:::api
            click InvProductAPI "https://github.com/marethy/e-commerce_microservice_aspnetcore/blob/main/src/Services/Inventory/Inventory.Product.API"
        end

        subgraph "Ordering Services"
            direction TB
            OrderingAPI["Ordering.API<br/>(SQL Server + RabbitMQ Consumer)"]:::api
            click OrderingAPI "https://github.com/marethy/e-commerce_microservice_aspnetcore/blob/main/src/Services/Ordering/Ordering.API"
            OrderingApp["Ordering.Application<br/>(CQRS, MediatR)"]:::shared
            click OrderingApp "https://github.com/marethy/e-commerce_microservice_aspnetcore/blob/main/src/Services/Ordering/Ordering.Application"
            OrderingDomain["Ordering.Domain<br/>(DDD Entities)"]:::shared
            click OrderingDomain "https://github.com/marethy/e-commerce_microservice_aspnetcore/blob/main/src/Services/Ordering/Ordering.Domain"
            OrderingInfra["Ordering.Infrastructure<br/>(EF Core)"]:::shared
            click OrderingInfra "https://github.com/marethy/e-commerce_microservice_aspnetcore/blob/main/src/Services/Ordering/Ordering.Infrastructure"
        end

        HangfireAPI["Hangfire.API<br/>(Background Jobs)"]:::api
        click HangfireAPI "https://github.com/marethy/e-commerce_microservice_aspnetcore/blob/main/src/Services/Hangfire.API"
    end

    %% Shared Building Blocks
    subgraph "Shared BuildingBlocks" 
        direction TB
        Contracts["Shared Contracts"]:::shared
        click Contracts "https://github.com/marethy/e-commerce_microservice_aspnetcore/tree/main/src/BuildingBlocks/Contracts"
        EventBus["Shared EventBus"]:::shared
        click EventBus "https://github.com/marethy/e-commerce_microservice_aspnetcore/tree/main/src/BuildingBlocks/EventBus"
        Infra["Shared Infrastructure"]:::shared
        click Infra "https://github.com/marethy/e-commerce_microservice_aspnetcore/tree/main/src/BuildingBlocks/Infrastructure"
        Logging["Common Logging"]:::shared
        click Logging "https://github.com/marethy/e-commerce_microservice_aspnetcore/blob/main/src/BuildingBlocks/Common.Logging"
        SharedDTOs["Shared DTOs & Utilities"]:::shared
        click SharedDTOs "https://github.com/marethy/e-commerce_microservice_aspnetcore/tree/main/src/BuildingBlocks/Shared"
    end

    %% Infrastructure Services
    subgraph "Infrastructure"
        direction TB
        RabbitMQ[("RabbitMQ")]:::mq
        Elasticsearch[("Elasticsearch")]:::infra
        Kibana[("Kibana")]:::infra
        Portainer[("Portainer")]:::infra
    end

    %% Databases
    subgraph "Databases"
        direction TB
        MySQL[(MySQL)]:::db
        PostgreSQL[(PostgreSQL)]:::db
        Redis[(Redis)]:::db
        MongoDB[(MongoDB)]:::db
        SQLServer[(SQL Server)]:::db
    end

    %% Connections: Gateway to services
    API_Gateway -->|"/products"| ProductAPI
    API_Gateway -->|"/customers"| CustomerAPI
    API_Gateway -->|"/baskets"| BasketAPI
    API_Gateway -->|"/inventory"| InvProductAPI
    API_Gateway -->|"/orders"| OrderingAPI
    API_Gateway -->|"/hangfire"| HangfireAPI

    %% Services to databases
    ProductAPI -->|reads/writes| MySQL
    CustomerAPI -->|reads/writes| PostgreSQL
    BasketAPI -->|reads/writes| Redis
    InvGrpc -->|reads/writes| MongoDB
    InvProductAPI -->|reads/writes| MongoDB
    OrderingAPI -->|reads/writes| SQLServer

    %% Synchronous calls
    BasketAPI -->|"gRPC: CheckStock"| InvGrpc

    %% Asynchronous messaging
    BasketAPI -.->|BasketCheckoutEvent| RabbitMQ
    RabbitMQ -.->|BasketCheckoutEvent| OrderingAPI

    %% Ordering layered dependencies
    OrderingAPI --> OrderingApp
    OrderingApp --> OrderingDomain
    OrderingApp --> OrderingInfra
    OrderingApp --> SharedDTOs

    %% Shared usage
    ProductAPI -.-> Contracts
    CustomerAPI -.-> Contracts
    BasketAPI -.-> Contracts
    InvGrpc -.-> Contracts
    InvProductAPI -.-> Contracts
    OrderingAPI -.-> Contracts
    HangfireAPI -.-> Contracts

    ProductAPI -.-> Logging
    CustomerAPI -.-> Logging
    BasketAPI -.-> Logging
    InvGrpc -.-> Logging
    InvProductAPI -.-> Logging
    OrderingAPI -.-> Logging
    HangfireAPI -.-> Logging

    ProductAPI -.-> Infra
    CustomerAPI -.-> Infra
    BasketAPI -.-> Infra
    InvGrpc -.-> Infra
    InvProductAPI -.-> Infra
    OrderingAPI -.-> Infra
    HangfireAPI -.-> Infra

    %% Infra to other
    RabbitMQ --> Elasticsearch
    RabbitMQ --> Kibana
    RabbitMQ --> Portainer

    %% Styles
    classDef api fill:#d1e8ff,stroke:#3399ff,stroke-width:2px;
    classDef frontend fill:#e8f4ff,stroke:#66a3ff,stroke-width:2px;
    classDef gateway fill:#fff2cc,stroke:#ffcc00,stroke-width:2px;
    classDef shared fill:#f0f0f0,stroke:#999999,stroke-width:1px,stroke-dasharray: 2 2;
    classDef infrared fill:#f9e6e6,stroke:#cc0000,stroke-width:2px;
    classDef infra fill:#e6f9ed,stroke:#00cc66,stroke-width:2px;
    classDef mq fill:#ffffff,stroke:#9966cc,stroke-width:2px,stroke-dasharray: 5 5;
    classDef db fill:#ffe6e6,stroke:#cc0000,stroke-width:2px;
    classDef external fill:#ffffff,stroke:#666666,stroke-width:1px;
```
