# gporto95/runtracker architecture

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

- Interactive diagram: https://gitdiagram.com/gporto95/runtracker
- Repository: https://github.com/gporto95/runtracker

## Overview

##### Main Components
- Web API Layer – Represented by the src/Web.Api folder. This holds all the endpoints (e.g., UserEndpoint.cs, WorkoutEndpoints.cs) along with middleware, global exception handling, request logging, and OpenAPI configuration. This is your presentation layer that handles HTTP requests.

- Application Layer – Found under src/Application. This layer follows a CQRS (Command Query Responsibility Segregation) pattern and includes Command/Query objects, their corresponding handlers, and several pipeline behaviors (for exception handling, logging, caching, validation, transactions). It acts as the intermediary between your presentation layer and the domain/business logic.

- Domain Layer – Located in src/Domain. This layer represents the business logic, entities (like User, Workout, Follower), domain events, and domain services. It encapsulates the core rules and invariants of the application.

- Infrastructure Layer – Under src/Infrastructure. This layer implements cross-cutting concerns such as database access (e.g., ApplicationReadDbContext, ApplicationWriteDbContext, repository implementations), caching, event bus, notifications, and the outbox pattern. It provides concrete implementations for interfaces defined in the Application and Domain layers and manages integration with external systems (e.g., database, external services, health checks).

- Shared Kernel – Contained in src/SharedKernel. This common library holds shared abstractions, such as error handling, domain event interfaces, date/time providers, and utility methods used throughout other layers.

##### Relationships and Interactions
- The Web API receives HTTP requests and maps them to endpoints. These endpoints translate client requests into application commands and queries.
- The Application layer processes these commands/queries using the pipeline behaviors. It coordinates validation, logging, caching, exception handling, and eventual interaction with both the Domain model and Infrastructure.
- Business logic resides in the Domain layer, where domain entities and services handle the core functionality (such as creating users, managing workouts, handling followers).
- Infrastructure provides the persistent storage and cross‑platform services (data contexts, repositories, caching services, messaging/event bus) that support Application and Domain operations.
- The Shared Kernel supplies common functionality that all layers rely on, ensuring consistency (for example, error handling and validation rules).

##### Architectural Patterns and Design Principles
- Layered Architecture and Domain‑Driven Design (DDD) – The clear separation into layers (Web API, Application, Domain, Infrastructure, and Shared Kernel) reflects typical DDD practices.
- CQRS – Evident in the command/query separation with distinct command handlers (e.g., CreateUserCommandHandler, CreateWorkoutCommandHandler) and query handlers.
- Pipeline Behaviors – Use of multiple pipeline behaviors (logging, caching, exception handling, validation, transactions) ensures cross‑cutting concerns are consistently managed.
- Event-Driven Communication – The presence of domain and integration events (e.g., FollowerCreatedDomainEvent, WorkoutRemovedIntegrationEvent) as well as an in‑memory message queue illustrate event-driven aspects.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Web API Layer
    subgraph "Web API Layer"
        API["HTTP Endpoints"]:::webapi
        subgraph "Endpoints"
            UEP["UserEndpoint.cs"]:::webapi
            WEP["WorkoutEndpoints.cs"]:::webapi
        end
        API --> UEP
        API --> WEP
    end

    %% Application Layer
    subgraph "Application Layer"
        PB["Pipeline Behaviors"]:::application
        CQ["CQRS Handlers"]:::application
        PB --> CQ
    end

    %% Domain Layer
    subgraph "Domain Layer"
        BL["Business Logic (Entities, Domain Events, Services)"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        DC["Data Contexts"]:::infrastructure
        EV["Event Bus & Outbox"]:::infrastructure
        REPO["Repositories"]:::infrastructure
    end

    %% Shared Kernel
    subgraph "Shared Kernel"
        SK["Shared Abstractions & Utilities"]:::shared
    end

    %% Data Flow Connections
    API -->|"sendsRequest"| PB
    CQ -->|"invokes"| BL
    BL -->|"persistsTo"| DC
    BL -->|"emitsEvents"| EV
    BL -->|"usesRepo"| REPO

    %% Shared Kernel Dependencies (dashed arrows)
    API -.->|"uses"| SK
    PB  -.->|"uses"| SK
    CQ  -.->|"uses"| SK
    BL  -.->|"uses"| SK

    %% Styles for nodes
    classDef webapi fill:#fce3ff,stroke:#ff66cc,stroke-width:2px;
    classDef application fill:#e0f7fa,stroke:#00acc1,stroke-width:2px;
    classDef domain fill:#e8f5e9,stroke:#43a047,stroke-width:2px;
    classDef infrastructure fill:#fff3e0,stroke:#ff9800,stroke-width:2px;
    classDef shared fill:#e1bee7,stroke:#8e24aa,stroke-width:2px;

    %% Click Events
    click API "https://github.com/gporto95/runtracker/blob/main/src/Web.Api"
    click UEP "https://github.com/gporto95/runtracker/blob/main/src/Web.Api/Endpoints/UserEndpoint.cs"
    click WEP "https://github.com/gporto95/runtracker/blob/main/src/Web.Api/Endpoints/WorkoutEndpoints.cs"
    click PB "https://github.com/gporto95/runtracker/tree/main/src/Application/Abstractions/Behaviors/"
    click CQ "https://github.com/gporto95/runtracker/tree/main/src/Application"
    click BL "https://github.com/gporto95/runtracker/tree/main/src/Domain"
    click DC "https://github.com/gporto95/runtracker/tree/main/src/Infrastructure/Data"
    click EV "https://github.com/gporto95/runtracker/tree/main/src/Infrastructure/Events"
    click REPO "https://github.com/gporto95/runtracker/tree/main/src/Infrastructure/Repositories"
    click SK "https://github.com/gporto95/runtracker/tree/main/src/SharedKernel"
```
