# ardalis/ddd-guestbook architecture

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

- Interactive diagram: https://gitdiagram.com/ardalis/ddd-guestbook
- Repository: https://github.com/ardalis/ddd-guestbook

## Overview

1. CleanArchitecture.Core – This is the heart of the system. It holds the domain layer containing entities (e.g., Guestbook, GuestbookEntry, ToDoItem), domain events, handlers, specifications, and shared kernel aspects. It defines the business rules and core logic with minimal external dependencies.

2. CleanArchitecture.Infrastructure – This layer implements the interfaces defined in the Core. It contains data access code (using Entity Framework Core), migration scripts, service implementations (email sender, domain event dispatcher), and dependency registration setups.

3. CleanArchitecture.Web – This is the user interface and entry point (an ASP.NET Core web project). It contains controllers (both API and MVC), pages, views, and configuration files (appsettings.json, startup settings). It also integrates the dependency injection container (StructureMap) to wire up the application’s components.

Additionally, there are several test projects (unit, functional, and integration) confirming the layered design and ensuring the system functions as expected.

A. Identify the Main Components:

- Domain Layer (Core Project)
- Domain Entities: Guestbook, GuestbookEntry, ToDoItem.
- Domain Events: EntryAddedEvent, ToDoItemCompletedEvent.
- Handlers: e.g., GuestbookNotificationHandler, ItemCompletedEmailNotificationHandler.
- Services and Specifications.
- Shared Kernel components (ValueObject, BaseEntity, etc.).

- Infrastructure Layer
- Data Access: AppDbContext, EF based repository implementations, EF Migrations.
- Domain Event Dispatcher and Email Message Sender implementations.
- Container Setup/Dependency Injection configuration.

- Web (Presentation) Layer
- API Controllers: GuestbookController, ToDoItemsController, MetaController.
- MVC Controllers & Views: HomeController, ToDoController, Razor Pages.
- Startup and Program configuration, along with static assets (wwwroot).

- External/Supporting Components
- External services such as email providers (via EmailMessageSenderService).
- CI/CD pipeline tools (Azure Pipelines configuration).
- Test Projects (Unit, Functional, Integration tests).

B. Determine Relationships and Interactions Between Components:

- The Web (Presentation) Layer depends on the Core Layer to receive business logic. It calls into controllers which either communicate directly with domain services or use API Models (DTOs) to transfer data.
- The Infrastructure Layer bridges between external resources (like the database or external APIs) and the Core. The Domain Layer defines interfaces (e.g., IRepository, IMessageSender) which the Infrastructure implements.
- Dependency inversion is central: higher-level modules (Core) shouldn’t depend on lower-level modules (Infrastructure) directly; instead, the Infrastructure implements interfaces defined in Core.
- Domain events flow out from the Domain Layer then are dispatched via the Infrastructure (DomainEventDispatcher) to trigger side effects (like email notifications).

C. Highlight Architectural Patterns and Design Principles:

- Domain-Driven Design (DDD): Focus on encapsulating business rules within entities, value objects, and domain services.
- Dependency Injection and Inversion: Use dependency injection frameworks (StructureMap and/or Autofac) to decouple layers.
- Separation of Concerns: Each layer is strictly responsible for its tasks (Core for business rules, Infrastructure for data persistency and external calls, and Web for UI concerns).

D. Include Relevant Technologies and Frameworks:

- ASP.NET Core for the Web layer.
- Entity Framework Core (with SQL Server as an example) for data access.
- StructureMap (or similar DI container) for dependency management.
- xUnit, Moq, and Microsoft.AspNetCore.TestHost in the test projects.
- Additional tools such as Azure Pipelines (CI/CD) are also part of the project’s broader ecosystem.

- Since this is a full-stack web application using Clean Architecture, focus on clearly separating the presentation (Web) part from the business logic (Core) and from external integrations (Infrastructure).
- Annotations or a key that notes the technology choices (ASP.NET Core, EF Core, StructureMap, etc.) associated with each layer.

- Use clear, descriptive labels such as “Domain (Core)”, “Infrastructure”, “Web (Presentation)”, and “Tests”.
- Optionally, add a segment for “CI/CD” showing Azure Pipelines as part of the development and deployment processes.

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Domain (Core)"
        DomainEntities["Domain Entities\n(Guestbook, GuestbookEntry, ToDoItem)"]
        DomainEvents["Domain Events\n(EntryAddedEvent,ToDoItemCompletedEvent)"]
        DomainHandlers["Domain Handlers\n(GuestbookNotificationHandler,ItemCompletedEmailNotificationHandler)"]
        DomainServices["Domain Services"]
        DomainSpecifications["Specifications"]
        SharedKernel["Shared Kernel"]
    end

    subgraph "Infrastructure"
        subgraph "Data Access"
            AppDbContext["AppDbContext"]
            EfRepository["EF Repository"]
            Migrations["Migrations"]
        end
        EventDispatcher["Domain Event Dispatcher"]
        EmailService["Email Message Sender"]
        ContainerSetup["Container Setup"]
        StartupSetup["Startup Setup"]
    end

    subgraph "Web (Presentation)"
        APIControllers["API Controllers"]
        MVCControllers["MVC Controllers"]
        Views["Views"]
        RazorPages["Razor Pages"]
        ProgramNode["Program"]
        StartupNode["Startup"]
        Configuration["Configuration Files"]
        StaticAssets["Static Assets (wwwroot)"]
    end

    subgraph "Tests"
        UnitTests["Unit Tests"]
        FunctionalTests["Functional Tests"]
        IntegrationTests["Integration Tests"]
    end

    subgraph "CI/CD"
        CICD["Azure Pipelines"]
    end

    %% Inter-layer Relationships
    APIControllers -->|"calls"| DomainServices
    MVCControllers -->|"calls"| DomainServices

    DomainEntities -->|"triggers"| DomainEvents
    DomainEvents -->|"handledBy"| DomainHandlers
    DomainEvents -->|"dispatchedBy"| EventDispatcher
    DomainEvents -->|"notifies"| EmailService

    EfRepository -->|"persists"| DomainEntities
    AppDbContext -->|"supports"| EfRepository

    ContainerSetup -->|"wires"| APIControllers
    StartupSetup -->|"wires"| MVCControllers

    DomainServices -->|"uses"| DomainSpecifications

    %% Web Startup Flow
    Configuration -->|"configures"| ProgramNode
    ProgramNode -->|"initiates"| StartupNode

    %% Testing Relationships
    UnitTests -->|"tests"| DomainServices
    FunctionalTests -->|"tests"| APIControllers
    IntegrationTests -->|"tests"| EfRepository

    CICD -->|"runs"| UnitTests
    CICD -->|"runs"| FunctionalTests
    CICD -->|"runs"| IntegrationTests

    %% Styling Classes
    class DomainEntities,DomainEvents,DomainHandlers,DomainServices,DomainSpecifications,SharedKernel domain
    class AppDbContext,EfRepository,Migrations,EventDispatcher,EmailService,ContainerSetup,StartupSetup infra
    class APIControllers,MVCControllers,Views,RazorPages,ProgramNode,StartupNode,Configuration,StaticAssets web
    class UnitTests,FunctionalTests,IntegrationTests test
    class CICD cicd

    %% Click Events for Domain Layer
    click DomainEntities "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Core/Entities"
    click DomainEvents "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Core/Events"
    click DomainHandlers "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Core/Handlers"
    click DomainServices "https://github.com/ardalis/ddd-guestbook/blob/master/src/CleanArchitecture.Core/Services/SomeDomainService.cs"
    click DomainSpecifications "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Core/Specifications"
    click SharedKernel "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Core/SharedKernel"

    %% Click Events for Infrastructure Layer
    click AppDbContext "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Infrastructure/Data"
    click EfRepository "https://github.com/ardalis/ddd-guestbook/blob/master/src/CleanArchitecture.Infrastructure/Data/EfRepository.cs"
    click Migrations "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Infrastructure/Data/Migrations"
    click EventDispatcher "https://github.com/ardalis/ddd-guestbook/blob/master/src/CleanArchitecture.Infrastructure/DomainEvents/DomainEventDispatcher.cs"
    click EmailService "https://github.com/ardalis/ddd-guestbook/blob/master/src/CleanArchitecture.Infrastructure/Services/EmailMessageSenderService.cs"
    click ContainerSetup "https://github.com/ardalis/ddd-guestbook/blob/master/src/CleanArchitecture.Infrastructure/ContainerSetup.cs"
    click StartupSetup "https://github.com/ardalis/ddd-guestbook/blob/master/src/CleanArchitecture.Infrastructure/StartupSetup.cs"

    %% Click Events for Web Layer
    click APIControllers "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Web/Api"
    click MVCControllers "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Web/Controllers"
    click Views "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Web/Views"
    click RazorPages "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Web/Pages"
    click ProgramNode "https://github.com/ardalis/ddd-guestbook/blob/master/src/CleanArchitecture.Web/Program.cs"
    click StartupNode "https://github.com/ardalis/ddd-guestbook/blob/master/src/CleanArchitecture.Web/Startup.cs"
    click Configuration "https://github.com/ardalis/ddd-guestbook/blob/master/src/CleanArchitecture.Web/appsettings.json"
    click StaticAssets "https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Web/wwwroot"

    %% Click Events for Tests
    click UnitTests "https://github.com/ardalis/ddd-guestbook/blob/master/tests/CleanArchitecture.Tests"
    click FunctionalTests "https://github.com/ardalis/ddd-guestbook/blob/master/tests/CleanArchitecture.FunctionalTests"
    click IntegrationTests "https://github.com/ardalis/ddd-guestbook/blob/master/tests/CleanArchitecture.IntegrationTests"

classDef domain fill:#FFD700,stroke:#000,stroke-width:2px;
classDef infra fill:#7FFFD4,stroke:#000,stroke-width:2px;
classDef web fill:#ADD8E6,stroke:#000,stroke-width:2px;
classDef test fill:#FFB6C1,stroke:#000,stroke-width:2px;
classDef cicd fill:#D3D3D3,stroke:#000,stroke-width:2px;
```
