# sklinkert/go-ddd architecture

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

- Interactive diagram: https://gitdiagram.com/sklinkert/go-ddd
- Repository: https://github.com/sklinkert/go-ddd

## Overview

##### Main Components
- Entities (e.g., Product, Seller, ValidatedProduct, ValidatedSeller under internal/domain/entities)
- Domain repositories interfaces (located under internal/domain/repositories)
- Application Layer: Acts as the mediator between the Domain and external interfaces. It includes:
- Commands, Queries, and use-case specific operations (under internal/application/command, internal/application/query, and services)
- Mappers to transform data between layers (internal/application/mapper)
- Infrastructure Layer: Provides technical capabilities such as database access:
- Database connectivity and concrete repository implementations (under internal/infrastructure/db with separate folders for Postgres and SQLite tests)
- Interface Layer: The external-facing API which exposes the system to users or external systems:
- REST API controllers and DTOs (under internal/interface/api/rest and its subdirectories for requests, responses, and mappers)

##### Relationships and Interactions
- The Domain layer should be independent and does not depend on other layers.
- The Application layer uses the Domain layer to execute business logic and enforces rules by handling commands, queries, and validations.
- The Infrastructure layer implements the persistence details as defined by the Domain (and sometimes Application) interfaces.
- The Interface layer (REST API) communicates with the Application layer to handle client requests and transform external demands into commands or queries.

##### Architectural Patterns & Design Principles
- Onion Architecture/Layered DDD: Emphasize how the Domain is central and all other layers depend inward toward it.
- Separation of concerns and dependency inversion: Domain knows nothing about Infrastructure, but domain’s interface contracts are implemented there.
- The use of clear boundaries to allow potential future migration to a microservices architecture.

##### Technologies and Components
- Golang as the primary programming language.
- The repository might include tooling via GitHub workflows (for CI/CD) and dependency management with go.mod and go.sum.
- REST API endpoints to expose the service (via controllers in internal/interface/api/rest).
- Database technology specifics: Postgres in production (and SQLite tests for in-memory validation).

##### Importance of Detailing the Architecture
- Critical components are represented.
- Use labels for clear identification (e.g., “SellerController”, “CreateSellerCommand”, “ProductRepository”).
- Where patterns like dependency inversion and separation of concerns are applied.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Interface Layer"
        I1["REST API Controllers & DTOs"]:::interface
    end

    subgraph "Application Layer"
        A1["Application Commands"]:::application
        A2["Application Queries"]:::application
        A3["Application Services"]:::application
        A4["Application Mappers"]:::application
    end

    subgraph "Domain Layer"
        D1["Domain Entities"]:::domain
        D2["Domain Repository Interfaces"]:::domain
    end

    subgraph "Infrastructure Layer"
        INF1["DB & Repo Implementations"]:::infrastructure
        INF2["DB Tests (SQLite)"]:::infrastructure
    end

    %% Data Flow
    I1 -->|"calls"| A1
    I1 -->|"calls"| A2
    I1 -->|"calls"| A3
    I1 -->|"calls"| A4

    A1 -->|"initiates"| D1
    A2 -->|"retrieves"| D1
    A3 -->|"executes_on"| D1
    A3 -->|"uses"| D2
    A4 -->|"transforms"| D1

    D2 -->|"implemented_in"| INF1
    INF1 -->|"validated_by"| INF2

    %% Click Events
    click I1 "https://github.com/sklinkert/go-ddd/tree/main/internal/interface/api/rest"
    click A1 "https://github.com/sklinkert/go-ddd/tree/main/internal/application/command"
    click A2 "https://github.com/sklinkert/go-ddd/tree/main/internal/application/query"
    click A3 "https://github.com/sklinkert/go-ddd/tree/main/internal/application/services"
    click A4 "https://github.com/sklinkert/go-ddd/tree/main/internal/application/mapper"
    click D1 "https://github.com/sklinkert/go-ddd/tree/main/internal/domain/entities"
    click D2 "https://github.com/sklinkert/go-ddd/tree/main/internal/domain/repositories"
    click INF1 "https://github.com/sklinkert/go-ddd/tree/main/internal/infrastructure/db"
    click INF2 "https://github.com/sklinkert/go-ddd/tree/main/internal/infrastructure/db_test/sqlite"

    %% Styles
    classDef interface fill:#FFAAAA,stroke:#333,stroke-width:2px,color:#000;
    classDef application fill:#66CCFF,stroke:#333,stroke-width:2px;
    classDef domain fill:#FFCC00,stroke:#333,stroke-width:2px;
    classDef infrastructure fill:#AAFFAA,stroke:#333,stroke-width:2px;
```
