# daohainam/microservice-patterns architecture

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

- Interactive diagram: https://gitdiagram.com/daohainam/microservice-patterns
- Repository: https://github.com/daohainam/microservice-patterns

## Overview

##### Project type and purpose
- This is an open‑source .NET solution (“MicroservicePatterns”) that provides:
- Core libraries (EventBus, DatabaseMigrationHelpers, ServiceDefaults, Shared, AppHost)
- Sample microservice implementations demonstrating key patterns (CQRS, Event Sourcing, Idempotent Consumer, Saga, Transactional Outbox, WebHook)
- Integration tests and unit tests validating these patterns
- It’s not a single deployable app but a framework + catalog of pattern‑based service templates.

##### File‑structure analysis
- Top‑level core libraries:
- EventBus (with a Kafka extension) – messaging abstractions and publisher/handler implementations
- MicroservicePatterns.AppHost – common Program.cs, DI/Configuration extensions for bootstrapping services
- MicroservicePatterns.DatabaseMigrationHelpers – EF Core migration helpers
- MicroservicePatterns.ServiceDefaults – default middleware/services
- MicroservicePatterns.Shared – cross‑cutting types (Consts, Pagination)
- patterns/ – a folder of subfolders, each a “pattern” containing one or more example services plus integration‑events projects and infra code:
- CQRS (Library services: Book, Borrower, Borrowing, BorrowingHistory + IntegrationEvents)
- EventSourcing (Banking.AccountService + IntegrationEvents + Infrastructure/EventStore)
- IdempotentConsumer (Catalog); Saga (multiple business domains: OnlineStore, TripPlanner)
- TransactionalOutbox (Abstractions, Aspire.Debezium, Banking.AccountService example, Infrastructure, IntegrationEvents, Publisher, Consumer)
- WebHook (DeliveryService + DispatchService, EventConsumer, Infrastructure)
- tests/ – integration tests orchestrating multiple patterns; unit tests for Saga.

##### Detail
- Microservice’s key components: API layer, Domain/Entity, Infrastructure (DB context, event handlers)
- Flow of integration events through EventBus or transactional outbox
- Pattern‑specific processors (e.g., Saga state machine, outbox poller, event store appenders)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Libraries
    subgraph "Core Libraries"
        EB["EventBus"]:::lib
        EBK["EventBus.Kafka"]:::lib
        DM["DatabaseMigrationHelpers"]:::lib
        SD["ServiceDefaults"]:::lib
        SH["Shared"]:::lib
        AH["AppHost"]:::lib
    end

    %% Pattern Clusters
    subgraph "CQRS Pattern" 
        CQRS.Book["BookService"]:::pattern
        CQRS.Borrower["BorrowerService"]:::pattern
        CQRS.Borrowing["BorrowingService"]:::pattern
        CQRS.History["BorrowingHistoryService"]:::pattern
        CQRS.Events["CQRS IntegrationEvents"]:::pattern
    end

    subgraph "Event Sourcing Pattern"
        ES.Account["AccountService"]:::pattern
        ES.Infra["EventStore Infrastructure"]:::pattern
        ES.Events["Banking IntegrationEvents"]:::pattern
        ES.Notification["Notification Worker"]:::pattern
        ES.Seed["Shared SeedWork"]:::pattern
    end

    subgraph "Idempotent Consumer Pattern"
        IC.Catalog["CatalogService"]:::pattern
    end

    subgraph "Saga Pattern - OnlineStore"
        SO.Catalog["CatalogService"]:::pattern
        SO.Inventory["InventoryService"]:::pattern
        SO.Order["OrderService"]:::pattern
        SO.Payment["PaymentService"]:::pattern
        SO.Events["OnlineStore IntegrationEvents"]:::pattern
    end

    subgraph "Saga Pattern - TripPlanner"
        STP.Hotel["HotelService"]:::pattern
        STP.Payment["PaymentService"]:::pattern
        STP.Ticket["TicketService"]:::pattern
        STP.Trip["TripPlanningService"]:::pattern
        STP.Events["TripPlanner IntegrationEvents"]:::pattern
    end

    subgraph "Transactional Outbox Pattern"
        TO.Abst["Outbox Abstractions"]:::pattern
        TO.Debezium["Debezium Adapter"]:::pattern
        TO.Account["Banking AccountService"]:::pattern
        TO.Infra["Outbox Infrastructure"]:::pattern
        TO.Events["Outbox IntegrationEvents"]:::pattern
        TO.Consumer["MessageConsumingService"]:::pattern
        TO.Publisher["Polling Publisher"]:::pattern
    end

    subgraph "WebHook Pattern"
        WH.Delivery["DeliveryService"]:::pattern
        WH.Infra["DeliveryService Infrastructure"]:::pattern
        WH.Dispatch["DispatchService"]:::pattern
        WH.Consumer["EventConsumer"]:::pattern
    end

    %% Tests
    subgraph "Tests"
        Tests.Integration["IntegrationTests Suite"]:::test
        Tests.Saga["Saga.UnitTests"]:::test
        Tests.Helpers["Test Helpers"]:::test
    end

    %% External Systems
    Kafka["Kafka Cluster"]:::ext
    DebeziumSys["Debezium Connectors"]:::ext
    Postgres["PostgreSQL DBs"]:::db
    HTTPClients["HTTP Clients"]:::ext

    %% Dependencies - compile-time
    subgraph Dependencies
        CQRS.Book -->|uses| AH
        CQRS.Borrower -->|uses| AH
        CQRS.Borrowing -->|uses| AH
        CQRS.History -->|uses| AH
        ES.Account -->|uses| AH
        IC.Catalog -->|uses| AH
        SO.Catalog -->|uses| AH
        STP.Trip -->|uses| AH
        TO.Account -->|uses| AH
        WH.Delivery -->|uses| AH

        CQRS.Book -->|uses| SH
        ES.Account -->|uses| SH

        CQRS.Book -->|uses| SD
        ES.Account -->|uses| SD

        CQRS.Book -->|migrates| DM
        ES.Account -->|migrates| DM
    end

    %% Messaging interactions
    CQRS.Events -.->|publishes| Kafka
    ES.Account -.->|writes events to| ES.Infra
    ES.Infra -.->|stores in| Postgres
    TO.Account -.->|writes to outbox| Postgres
    TO.Account -.->|Debezium tails| DebeziumSys
    DebeziumSys -.->|notifies| TO.Publisher
    TO.Publisher -.->|publishes via| EBK
    TO.Consumer -.->|consumes from| Kafka
    SO.Events -.->|via EventBus| Kafka
    STP.Events -.->|via EventBus| Kafka
    IC.Catalog -.->|consumes from| Kafka
    WH.Consumer -.->|consumes| Kafka
    WH.Consumer -.->|writes to| Postgres
    WH.Dispatch -.->|HTTP calls| HTTPClients

    %% EventBus wiring
    EBK -->|extension of| EB

    %% Tests interactions
    Tests.Integration -.->|orchestrates via HTTP/Kafka| CQRS.Book
    Tests.Integration -.->|orchestrates via HTTP/Kafka| ES.Account
    Tests.Saga -.->|validates| SO.Order

    %% Click Events
    click EB "https://github.com/daohainam/microservice-patterns/tree/master/EventBus"
    click EBK "https://github.com/daohainam/microservice-patterns/blob/master/EventBus.Kafka"
    click AH "https://github.com/daohainam/microservice-patterns/blob/master/MicroservicePatterns.AppHost"
    click DM "https://github.com/daohainam/microservice-patterns/blob/master/MicroservicePatterns.DatabaseMigrationHelpers"
    click SD "https://github.com/daohainam/microservice-patterns/blob/master/MicroservicePatterns.ServiceDefaults"
    click SH "https://github.com/daohainam/microservice-patterns/blob/master/MicroservicePatterns.Shared"
    click CQRS.Book "https://github.com/daohainam/microservice-patterns/blob/master/patterns/CQRS/CQRS.Library.BookService"
    click CQRS.Borrower "https://github.com/daohainam/microservice-patterns/blob/master/patterns/CQRS/CQRS.Library.BorrowerService"
    click CQRS.Borrowing "https://github.com/daohainam/microservice-patterns/blob/master/patterns/CQRS/CQRS.Library.BorrowingService"
    click CQRS.History "https://github.com/daohainam/microservice-patterns/blob/master/patterns/CQRS/CQRS.Library.BorrowingHistoryService"
    click CQRS.Events "https://github.com/daohainam/microservice-patterns/blob/master/patterns/CQRS/CQRS.Library.IntegrationEvents"
    click ES.Account "https://github.com/daohainam/microservice-patterns/blob/master/patterns/EventSourcing/EventSourcing.Banking.AccountService"
    click ES.Infra "https://github.com/daohainam/microservice-patterns/blob/master/patterns/EventSourcing/EventSourcing.Infrastructure"
    click ES.Events "https://github.com/daohainam/microservice-patterns/blob/master/patterns/EventSourcing/EventSourcing.Banking.IntegrationEvents"
    click ES.Notification "https://github.com/daohainam/microservice-patterns/blob/master/patterns/EventSourcing/EventSourcing.NotificationService"
    click ES.Seed "https://github.com/daohainam/microservice-patterns/blob/master/patterns/EventSourcing/EventSourcing.SeedWork"
    click IC.Catalog "https://github.com/daohainam/microservice-patterns/blob/master/patterns/IdempotentConsumer/IdempotentConsumer.CatalogService"
    click SO.Catalog "https://github.com/daohainam/microservice-patterns/blob/master/patterns/Saga/Saga.OnlineStore.CatalogService"
    click SO.Inventory "https://github.com/daohainam/microservice-patterns/blob/master/patterns/Saga/Saga.OnlineStore.InventoryService"
    click SO.Order "https://github.com/daohainam/microservice-patterns/blob/master/patterns/Saga/Saga.OnlineStore.OrderService"
    click SO.Payment "https://github.com/daohainam/microservice-patterns/blob/master/patterns/Saga/Saga.OnlineStore.PaymentService"
    click SO.Events "https://github.com/daohainam/microservice-patterns/blob/master/patterns/Saga/Saga.OnlineStore.IntegrationEvents"
    click STP.Hotel "https://github.com/daohainam/microservice-patterns/blob/master/patterns/Saga/Saga.TripPlanner.HotelService"
    click STP.Payment "https://github.com/daohainam/microservice-patterns/blob/master/patterns/Saga/Saga.TripPlanner.PaymentService"
    click STP.Ticket "https://github.com/daohainam/microservice-patterns/blob/master/patterns/Saga/Saga.TripPlanner.TicketService"
    click STP.Trip "https://github.com/daohainam/microservice-patterns/blob/master/patterns/Saga/Saga.TripPlanner.TripPlanningService"
    click STP.Events "https://github.com/daohainam/microservice-patterns/blob/master/patterns/Saga/Saga.TripPlanner.IntegrationEvents"
    click TO.Abst "https://github.com/daohainam/microservice-patterns/blob/master/patterns/TransactionalOutbox/TransactionalOutbox.Abstractions"
    click TO.Debezium "https://github.com/daohainam/microservice-patterns/blob/master/patterns/TransactionalOutbox/TransactionalOutbox.Aspire.Debezium"
    click TO.Account "https://github.com/daohainam/microservice-patterns/blob/master/patterns/TransactionalOutbox/TransactionalOutbox.Banking.AccountService"
    click TO.Infra "https://github.com/daohainam/microservice-patterns/blob/master/patterns/TransactionalOutbox/TransactionalOutbox.Infrastructure"
    click TO.Events "https://github.com/daohainam/microservice-patterns/blob/master/patterns/TransactionalOutbox/TransactionalOutbox.IntegrationEvents"
    click TO.Consumer "https://github.com/daohainam/microservice-patterns/blob/master/patterns/TransactionalOutbox/TransactionalOutbox.MessageConsumingService"
    click TO.Publisher "https://github.com/daohainam/microservice-patterns/blob/master/patterns/TransactionalOutbox/TransactionalOutbox.Publisher.Polling"
    click WH.Delivery "https://github.com/daohainam/microservice-patterns/blob/master/patterns/WebHook/WebHook.DeliveryService"
    click WH.Infra "https://github.com/daohainam/microservice-patterns/blob/master/patterns/WebHook/WebHook.DeliveryService.Infrastructure"
    click WH.Dispatch "https://github.com/daohainam/microservice-patterns/blob/master/patterns/WebHook/WebHook.DeliveryService.DispatchService"
    click WH.Consumer "https://github.com/daohainam/microservice-patterns/blob/master/patterns/WebHook/WebHook.DeliveryService.EventConsumer"
    click Tests.Integration "https://github.com/daohainam/microservice-patterns/tree/master/tests/IntegrationTests"
    click Tests.Saga "https://github.com/daohainam/microservice-patterns/blob/master/tests/Saga.UnitTests"
    click Tests.Helpers "https://github.com/daohainam/microservice-patterns/tree/master/tests/TestHelpers"

    %% Styles
    classDef lib fill:#D0E6FF,stroke:#4A90E2;
    classDef pattern fill:#E2F7E1,stroke:#3CB371;
    classDef ext fill:#FFEFD5,stroke:#FF8C00;
    classDef db fill:#FFF2CC,stroke:#D4A017,stroke-width:2px;
    classDef test fill:#E0E0E0,stroke:#A9A9A9;
```
