# evgenirusev/.net-domain-driven-design-template architecture

> Architecture diagram of the GitHub repository evgenirusev/.net-domain-driven-design-template, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/evgenirusev/.net-domain-driven-design-template
- Repository: https://github.com/evgenirusev/.net-domain-driven-design-template

## Overview

##### Project Type and Purpose
- This is a monolithic .NET Core 8 solution using Clean Architecture and Domain-Driven Design (DDD) by grouping multiple bounded contexts into one deployable.
- It exposes several HTTP APIs (Web projects) and uses EF Core, MediatR, FluentValidation, AutoMapper and Scrutor under the hood.

##### File Structure
- Top‐level “bounded contexts” each have four layers:
- Application (commands, queries, DTOs, MediatR handlers)
- Domain (entities, value objects, domain events, interfaces)
- Infrastructure (EF Core DbContext, Repositories, Migrations, Services)
- Web (ASP.NET Core controllers, model binders, middleware, configuration)
- A shared “Common” context provides cross‐cutting abstractions (ICurrentUser, IImageService, base Entity, events dispatcher, validation behavior).
- ProjectStartup is the composition root (Program.cs, DI registrations, appsettings.json, migrations script).

##### Main Components
- Common Kernel
- Bounded Contexts:
- Identity
- ProductCatalog
- OrderManagement
- Statistics
- Startup Host
- Database(s) (single or per‐context)
- External HTTP call from OrderManagement → ProductCatalog
- Internal Event Bus (MediatR / IEventDispatcher) for Domain Events (OrderAddedEvent, ProductAddedEvent)

##### Relationships & Interactions
- Startup loads all Web API projects into one ASP.NET Core host and wires DI (via Scrutor’s assembly scanning).
- Web controllers call Application layer handlers (MediatR).
- Application handlers call Domain factories/repositories or Infrastructure services (e.g. IJwtGenerator, IImageService)
- Infrastructure persists aggregates via EF Core DbContexts.
- Upon saving aggregates, DomainEvents are raised, published by EventDispatcher, and handled by other contexts (e.g. Statistics.Application’s OrderAddedEventHandler).
- OrderManagement.Application uses IProductCatalogHttpService to fetch product info from the ProductCatalog Web API.

##### Architectural Patterns & Design Principles
- Vertical Slices: each feature (command/query) is self‐contained within its Application folder.
- Layered Clean Architecture: Web → Application → Domain → Infrastructure
- Bounded Context Isolation: contexts do not directly reference each other’s Domain or Infrastructure, only via Events or HTTP APIs.
- Shared Kernel: the Common project holds cross‐cutting abstractions.
- CQRS: separate Commands from Queries in Application layer.
- Anti-Corruption Layers: Factories for entity creation, Repositories for data access, FluentValidation for input validation, DomainEvents for cross‐context integration.

##### Technologies & Frameworks
- .NET Core 8 / ASP.NET Core 8
- Entity Framework Core 8
- MediatR for in‐process messaging
- FluentValidation for command/query validation
- AutoMapper for DTO ↔ Domain mapping
- Scrutor for DI registration
- xUnit & FluentAssertions for tests

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Common Kernel
    subgraph "Common Kernel" 
        direction TB
        CA["Common.Application"]:::common
        CD["Common.Domain"]:::common
        CI["Common.Infrastructure"]:::common
        CW["Common.Web"]:::common
    end

    %% Bounded Context: Identity
    subgraph "Identity Context"
        direction TB
        IWeb["Identity.Web\n(Controllers, Config)"]:::web
        IApp["Identity.Application\n(Commands, Validators)"]:::app
        IDom["Identity.Domain\n(Entities, Interfaces)"]:::domain
        IInfra["Identity.Infrastructure\n(DbContext, Services)"]:::infra
    end

    %% Bounded Context: ProductCatalog
    subgraph "ProductCatalog Context"
        direction TB
        PWeb["ProductCatalog.Web\n(Controllers, Config)"]:::web
        PApp["ProductCatalog.Application\n(Commands, Queries)"]:::app
        PDom["ProductCatalog.Domain\n(Entities, Factories)"]:::domain
        PInfra["ProductCatalog.Infrastructure\n(DbContext, Repos)"]:::infra
    end

    %% Bounded Context: OrderManagement
    subgraph "OrderManagement Context"
        direction TB
        OWeb["OrderManagement.Web\n(Controllers, Config)"]:::web
        OApp["OrderManagement.Application\n(Commands, HTTPService)"]:::app
        ODom["OrderManagement.Domain\n(Entities, Factories)"]:::domain
        OInfra["OrderManagement.Infrastructure\n(DbContext, HttpService)"]:::infra
    end

    %% Bounded Context: Statistics
    subgraph "Statistics Context"
        direction TB
        SWeb["Statistics.Web\n(Controllers, Config)"]:::web
        SApp["Statistics.Application\n(Event Handlers)"]:::app
        SDom["Statistics.Domain\n(Entities)"]:::domain
        SInfra["Statistics.Infrastructure\n(DbContext, Repos)"]:::infra
    end

    %% Startup Host
    ProjectStartup["ProjectStartup\n(Program.cs, DI, migrations)"]:::common

    %% Databases
    subgraph Databases
        direction TB
        DB_Common(("CommonDB")):::database
        DB_Identity(("IdentityDB")):::database
        DB_Product(("ProductDB")):::database
        DB_Order(("OrderDB")):::database
        DB_Stats(("StatisticsDB")):::database
    end

    %% Event Bus & Domain Events
    EventBus["EventDispatcher\n(IEventDispatcher)"]:::event
    OrderAdded["OrderAddedEvent"]:::event
    ProductAdded["ProductAddedEvent"]:::event

    %% Relationships: Startup to Web
    ProjectStartup --> IWeb
    ProjectStartup --> PWeb
    ProjectStartup --> OWeb
    ProjectStartup --> SWeb

    %% Web to Application
    IWeb --> IApp
    PWeb --> PApp
    OWeb --> OApp
    SWeb --> SApp

    %% Application to Domain & Infrastructure
    IApp --> IDom
    IApp --> IInfra
    PApp --> PDom
    PApp --> PInfra
    OApp --> ODom
    OApp --> OInfra
    SApp --> SDom
    SApp --> SInfra

    %% Infrastructure to Databases
    IInfra --> DB_Identity
    PInfra --> DB_Product
    OInfra --> DB_Order
    SInfra --> DB_Stats
    CI --> DB_Common

    %% Shared Kernel DB
    CI --> DB_Common

    %% Event flow
    IDom --> EventBus
    PDom --> EventBus
    EventBus --> OrderAdded
    EventBus --> ProductAdded
    OrderAdded -.-> SApp
    ProductAdded -.-> SApp

    %% HTTP call from OrderManagement to ProductCatalog
    OApp -->|"HTTP: IProductCatalogHttpService"| PWeb

    %% Click Events for component mapping
    click CA "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Common/Common.Application"
    click CD "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Common/Common.Domain"
    click CI "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Common/Common.Infrastructure"
    click CW "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Common/Common.Web"
    click IWeb "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Identity/Identity.Web"
    click IApp "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Identity/Identity.Application"
    click IDom "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Identity/Identity.Domain"
    click IInfra "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Identity/Identity.Infrastructure"
    click PWeb "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/ProductCatalog/ProductCatalog.Web"
    click PApp "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/ProductCatalog/ProductCatalog.Application"
    click PDom "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/ProductCatalog/ProductCatalog.Domain"
    click PInfra "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/ProductCatalog/ProductCatalog.Infrastructure"
    click OWeb "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/OrderManagement/OrderManagement.Web"
    click OApp "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/OrderManagement/OrderManagement.Application"
    click ODom "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/OrderManagement/OrderManagement.Domain"
    click OInfra "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/OrderManagement/OrderManagement.Infrastructure"
    click SWeb "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Statistics/Statistics.Web"
    click SApp "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Statistics/Statistics.Application"
    click SDom "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Statistics/Statistics.Domain"
    click SInfra "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/Statistics/Statistics.Infrastructure"
    click ProjectStartup "https://github.com/evgenirusev/.net-domain-driven-design-template/blob/main/ProjectStartup/Program.cs"

    %% Styles
    classDef common fill:#d3d3d3,stroke:#555;
    classDef web fill:#cce5ff,stroke:#007bff;
    classDef app fill:#d4edda,stroke:#28a745;
    classDef domain fill:#fff3cd,stroke:#ffc107;
    classDef infra fill:#ffe5b4,stroke:#fd7e14;
    classDef database fill:#e2e3e5,stroke:#6c757d,stroke-dasharray: 4 2;
    classDef event fill:#f8d7da,stroke:#dc3545,stroke-dasharray: 2 2;
```
