# jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing architecture

> Architecture diagram of the GitHub repository jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing
- Repository: https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing

## Overview

This is a customer-management ASP.NET Core API demonstrating Clean Architecture, CQRS, and event sourcing. The primary workflow accepts customer create/update/delete requests, dispatches commands to application handlers, applies domain behavior, and persists changes; customer events also update the query-side model. Reads use separate query handlers and repositories, with caching for customer lookups. The tree and samples support the API, command/query, domain, event-projection, and cache paths shown; some infrastructure wiring and event-store internals are inferred only from names and README claims, so uncertain links are omitted.

Main entry points: [`src/Shop.Application/Customer/Handlers`](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/tree/main/src/Shop.Application/Customer/Handlers) (Command handlers), [`src/Shop.Query/EventHandlers/CustomerEventHandler.cs`](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/EventHandlers/CustomerEventHandler.cs) (Customer event handler), [`src/Shop.Query/Data/Repositories/CustomerReadOnlyRepository.cs`](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/Data/Repositories/CustomerReadOnlyRepository.cs) (Customer read repository), [`src/Shop.Infrastructure/Data/Services/DistributedCacheService.cs`](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Infrastructure/Data/Services/DistributedCacheService.cs) (Cache service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Public API"]
  node_customers_api["Customers controller"]
  node_mediator["MediatR dispatcher"]
end

subgraph group_write["Command and Domain"]
  node_commands["Customer commands<br/>[Commands]"]
  node_command_handlers["Command handlers<br/>[Handlers]"]
  node_command_validators["Command validators<br/>[Commands]"]
  node_customer_domain["Customer aggregate<br/>[Customer.cs]"]
  node_customer_factory["Customer factory<br/>[CustomerFactory.cs]"]
  node_write_repository["Customer write repository"]
  node_unit_of_work["Unit of work<br/>[UnitOfWork.cs]"]
  node_write_db[("Write database context<br/>[WriteDbContext.cs]")]
end

subgraph group_events["Events and Projection"]
  node_domain_events["Customer domain events<br/>[Events]"]
  node_event_store["Event store repository"]
  node_event_handler["Customer event handler"]
  node_event_mapper["Event projection mapping"]
end

subgraph group_read["Query Side"]
  node_sync_db["Query synchronization<br/>[ISynchronizeDb.cs]"]
  node_read_db[("Query database context<br/>[NoSqlDbContext.cs]")]
  node_query_model["Customer query model"]
  node_query_handlers["Customer query handlers<br/>[Handlers]"]
  node_read_repository["Customer read repository"]
  node_cache[("Cache service")]
end

node_client(("API client"))
node_cache_backend[("Cache backend")]

node_client -->|"sends HTTP"| node_customers_api
node_customers_api -->|"dispatches request"| node_mediator
node_mediator -->|"dispatches command"| node_command_handlers
node_mediator -->|"dispatches query"| node_query_handlers
node_command_handlers -->|"validates input"| node_command_validators
node_command_handlers -->|"checks and saves"| node_write_repository
node_command_handlers -->|"creates customer"| node_customer_factory
node_customer_factory -->|"constructs aggregate"| node_customer_domain
node_command_handlers -->|"commits changes"| node_unit_of_work
node_write_repository -->|"persists customer"| node_write_db
node_customer_domain -->|"raises events"| node_domain_events
node_unit_of_work -->|"stores events"| node_event_store
node_domain_events -->|"notifies handler"| node_event_handler
node_event_handler -->|"maps events"| node_event_mapper
node_event_handler -->|"upserts or deletes"| node_sync_db
node_sync_db -->|"updates projection"| node_read_db
node_event_handler -->|"invalidates keys"| node_cache
node_query_handlers -->|"requests customer data"| node_read_repository
node_query_handlers -->|"gets or creates"| node_cache
node_cache -->|"loads on miss"| node_read_repository
node_read_repository -->|"reads projection"| node_read_db
node_event_mapper -->|"builds model"| node_query_model
node_read_repository -->|"returns models"| node_query_model
node_cache -.->|"reads and writes"| node_cache_backend

click node_customers_api "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.PublicApi/Controllers/v1/CustomersController.cs"
click node_commands "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/tree/main/src/Shop.Application/Customer/Commands"
click node_command_handlers "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/tree/main/src/Shop.Application/Customer/Handlers"
click node_command_validators "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/tree/main/src/Shop.Application/Customer/Commands"
click node_customer_domain "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Domain/Entities/CustomerAggregate/Customer.cs"
click node_customer_factory "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Domain/Factories/CustomerFactory.cs"
click node_write_repository "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Infrastructure/Data/Repositories/CustomerWriteOnlyRepository.cs"
click node_unit_of_work "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Infrastructure/Data/UnitOfWork.cs"
click node_write_db "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Infrastructure/Data/Context/WriteDbContext.cs"
click node_domain_events "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/tree/main/src/Shop.Domain/Entities/CustomerAggregate/Events"
click node_event_store "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Infrastructure/Data/Repositories/EventStoreRepository.cs"
click node_event_handler "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/EventHandlers/CustomerEventHandler.cs"
click node_event_mapper "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/Profiles/EventToQueryModelProfile.cs"
click node_sync_db "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/Abstractions/ISynchronizeDb.cs"
click node_read_db "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/Data/Context/NoSqlDbContext.cs"
click node_query_model "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/QueriesModel/CustomerQueryModel.cs"
click node_query_handlers "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/tree/main/src/Shop.Query/Application/Customer/Handlers"
click node_read_repository "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/Data/Repositories/CustomerReadOnlyRepository.cs"
click node_cache "https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Infrastructure/Data/Services/DistributedCacheService.cs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_customers_api,node_mediator,node_client toneBlue
class node_commands,node_command_handlers,node_command_validators,node_customer_domain,node_customer_factory,node_write_repository,node_unit_of_work,node_write_db,node_cache_backend toneAmber
class node_domain_events,node_event_store,node_event_handler,node_event_mapper toneMint
class node_sync_db,node_read_db,node_query_model,node_query_handlers,node_read_repository,node_cache toneRose
```

## Components

### Public API

- [Customers controller](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.PublicApi/Controllers/v1/CustomersController.cs): `src/Shop.PublicApi/Controllers/v1/CustomersController.cs`
- **MediatR dispatcher**

### Command and Domain

- [Customer commands](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/tree/main/src/Shop.Application/Customer/Commands): `src/Shop.Application/Customer/Commands`
- [Command handlers](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/tree/main/src/Shop.Application/Customer/Handlers): `src/Shop.Application/Customer/Handlers`
- [Command validators](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/tree/main/src/Shop.Application/Customer/Commands): `src/Shop.Application/Customer/Commands`
- [Customer aggregate](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Domain/Entities/CustomerAggregate/Customer.cs): `src/Shop.Domain/Entities/CustomerAggregate/Customer.cs`
- [Customer factory](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Domain/Factories/CustomerFactory.cs): `src/Shop.Domain/Factories/CustomerFactory.cs`
- [Customer write repository](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Infrastructure/Data/Repositories/CustomerWriteOnlyRepository.cs): `src/Shop.Infrastructure/Data/Repositories/CustomerWriteOnlyRepository.cs`
- [Unit of work](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Infrastructure/Data/UnitOfWork.cs): `src/Shop.Infrastructure/Data/UnitOfWork.cs`
- [Write database context](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Infrastructure/Data/Context/WriteDbContext.cs): `src/Shop.Infrastructure/Data/Context/WriteDbContext.cs`

### Events and Projection

- [Customer domain events](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/tree/main/src/Shop.Domain/Entities/CustomerAggregate/Events): `src/Shop.Domain/Entities/CustomerAggregate/Events`
- [Event store repository](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Infrastructure/Data/Repositories/EventStoreRepository.cs): `src/Shop.Infrastructure/Data/Repositories/EventStoreRepository.cs`
- [Customer event handler](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/EventHandlers/CustomerEventHandler.cs): `src/Shop.Query/EventHandlers/CustomerEventHandler.cs`
- [Event projection mapping](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/Profiles/EventToQueryModelProfile.cs): `src/Shop.Query/Profiles/EventToQueryModelProfile.cs`

### Query Side

- [Query synchronization](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/Abstractions/ISynchronizeDb.cs): `src/Shop.Query/Abstractions/ISynchronizeDb.cs`
- [Query database context](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/Data/Context/NoSqlDbContext.cs): `src/Shop.Query/Data/Context/NoSqlDbContext.cs`
- [Customer query model](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/QueriesModel/CustomerQueryModel.cs): `src/Shop.Query/QueriesModel/CustomerQueryModel.cs`
- [Customer query handlers](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/tree/main/src/Shop.Query/Application/Customer/Handlers): `src/Shop.Query/Application/Customer/Handlers`
- [Customer read repository](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Query/Data/Repositories/CustomerReadOnlyRepository.cs): `src/Shop.Query/Data/Repositories/CustomerReadOnlyRepository.cs`
- [Cache service](https://github.com/jeangatto/asp.net-core-clean-architecture-cqrs-event-sourcing/blob/main/src/Shop.Infrastructure/Data/Services/DistributedCacheService.cs): `src/Shop.Infrastructure/Data/Services/DistributedCacheService.cs`

### Other components

- **API client**
- **Cache backend**

## Connections

- API client → Customers controller: sends HTTP
- Customers controller → MediatR dispatcher: dispatches request
- MediatR dispatcher → Command handlers: dispatches command
- MediatR dispatcher → Customer query handlers: dispatches query
- Command handlers → Command validators: validates input
- Command handlers → Customer write repository: checks and saves
- Command handlers → Customer factory: creates customer
- Customer factory → Customer aggregate: constructs aggregate
- Command handlers → Unit of work: commits changes
- Customer write repository → Write database context: persists customer
- Customer aggregate → Customer domain events: raises events
- Unit of work → Event store repository: stores events
- Customer domain events → Customer event handler: notifies handler
- Customer event handler → Event projection mapping: maps events
- Customer event handler → Query synchronization: upserts or deletes
- Query synchronization → Query database context: updates projection
- Customer event handler → Cache service: invalidates keys
- Customer query handlers → Customer read repository: requests customer data
- Customer query handlers → Cache service: gets or creates
- Cache service → Customer read repository: loads on miss
- Customer read repository → Query database context: reads projection
- Event projection mapping → Customer query model: builds model
- Customer read repository → Customer query model: returns models
- Cache service → Cache backend: reads and writes
