# mayorgacdev/stock-master-api architecture

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

- Interactive diagram: https://gitdiagram.com/mayorgacdev/stock-master-api
- Repository: https://github.com/mayorgacdev/stock-master-api

## Overview

##### Main Architectural Components
a. Presentation Layer (Web API)
- Located in the “src/Training.WebApi” folder.
- Contains Controllers (e.g., CustomersController.cs, WarehouseController.cs), Middleware (TrainingMiddleware.cs), configuration files (appsettings.json), and the Program.cs startup logic.
- Purpose: Expose endpoints for clients to interact with the system.

b. Application Layer
- Found under “src/Training.Application.”
- Organizes business logic including service classes (CustomerService, WarehouseService), request classes (create and validation requests for Customers, Products, Warehouse, etc.), mappings, attributes, validators, and extension methods.
- Purpose: Implement use cases, validations, and orchestrate transactions. This layer acts as the intermediary between the UI (Web API) and the Domain/Infrastructure layers.

c. Domain Layer
- Present in “src/Training.Domain.”
- Contains the core business entities and domain models (e.g., Customer, Product, ProductBrand, Warehouse, etc.), along with common domain value objects (Money, Currency) and utility classes.
- Purpose: Represent the core business logic and rules independent of external concerns.

d. Infrastructure Layer
- Found in “src/Training.Infraestructure.”
- Handles data access through the repository pattern, Entity Framework configurations (including DbContext, configurations for Inventory and Sales entities, migrations), and other system integrations.
- Purpose: Provide implementations for persistence, external data communication, and interface adherence (e.g., IRepository, IUnitOfWork).

e. Common/Shared Layer
- Located under “src/Training.Common.”
- Contains shared utilities such as extension methods, common response formats (Response, PagedResponse, ErrorResponse), pagination, and filtering functionality.
- Purpose: Reduce duplication and centralize logic that is used across multiple layers.

##### Establish Relationships and Data Flow
- The Web API (Presentation Layer) serves as the entry point. It receives HTTP requests and routes them to the appropriate controllers.
- These controllers interact with the Application Layer (via services and requests) to execute business logic.
- The Application Layer uses domain entities defined in the Domain Layer to enforce business rules.
- For data persistence or external data operations, the Application Layer calls upon the Infrastructure Layer, which implements repository interfaces and handles Entity Framework operations.
- The Common Layer is used by all other layers to provide shared functionalities (e.g., handling responses, applying filters).

##### Architectural Patterns and Design Principles
- Layered/Clean Architecture: The separation of Presentation, Application, Domain, and Infrastructure is a clear indicator of a layered design that decouples concerns.
- Domain-Driven Design (DDD): The existence of a “Training.Domain” with entities, value objects, and domain logic suggests a DDD approach.
- Use of Repository and Unit of Work Patterns: Embedded in the Infrastructure layer are repository base classes, specification patterns, and unit-of-work interfaces.
- Extension and Attribute-Based Programming: The Application layer has custom attributes (e.g., ServiceAttribute, ValidatorAttribute) used presumably for cross-cutting concerns such as logging, validation, or automatic interface generation.

##### Final Assembly
- Next, partition the layers vertically or horizontally following the dependency rule – inner layers (Domain, Common) at the core, with external layers (Application, Infrastructure, Web API) branching outward.
- That every key sub-component from the file tree is captured as part of its corresponding layer. For instance, note that “Training.Application/Requests” contains specific requests, and “Training.Infraestructure/Data” includes data access and specification logic.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Actor
    Client["Client/HTTP Request"]:::client

    %% Presentation Layer (Web API)
    subgraph "Presentation Layer (Web API)"
        P1["\"CustomersController.cs\""]:::presentation
        P2["\"WarehouseController.cs\""]:::presentation
        P3["\"TrainingMiddleware.cs\""]:::presentation
        P4["\"appsettings.json\""]:::presentation
        P5["\"Program.cs\""]:::presentation
    end

    %% Application Layer
    subgraph "Application Layer"
        A1["\"CustomerService.cs\""]:::application
        A2["\"WarehouseService.cs\""]:::application
        A3["\"MappingProfile.cs\""]:::application
        A4["Requests"]:::application
        A5["Validations"]:::application
        A6["Attributes"]:::application
        A7["Extensions"]:::application
    end

    %% Domain Layer
    subgraph "Domain Layer"
        D1["Inventory"]:::domain
        D2["Sales"]:::domain
        D3["Common"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        I1["Data"]:::infrastructure
        I2["\"TrainingDbContext.cs\""]:::infrastructure
        I3["Migrations"]:::infrastructure
        I4["Interfaces"]:::infrastructure
        I5["Inventory (EF Config)"]:::infrastructure
        I6["Sales (EF Config)"]:::infrastructure
    end

    %% Common/Shared Layer
    subgraph "Common Layer"
        C1["Extensions"]:::common
        C2["Responses"]:::common
        C3["ServicesFilter"]:::common
    end

    %% Data Flow from Client to Presentation Layer
    Client -->|"sendsRequest"| P1
    Client -->|"sendsRequest"| P2
    Client -->|"sendsRequest"| P3
    Client -->|"sendsRequest"| P4
    Client -->|"sendsRequest"| P5

    %% Presentation Layer to Application Layer (Controllers calling services)
    P1 -->|"callsService"| A1
    P2 -->|"callsService"| A2
    P3 -->|"invokesLogic"| A1

    %% Application Layer interactions
    A1 -->|"appliesRule"| D1
    A1 -->|"appliesRule"| D2
    A2 -->|"appliesRule"| D3
    A1 -->|"persistsData"| I1
    A2 -->|"persistsData"| I1
    A3 -->|"mapsData"| I2

    %% Usage of Common Layer across components
    P1 ---|"uses"| C1
    A1 ---|"uses"| C1
    D1 ---|"uses"| C1
    I1 ---|"uses"| C1

    A1 ---|"formats"| C2
    P1 ---|"filters"| C3

    %% Click Events for Presentation Layer
    click P1 "https://github.com/mayorgacdev/stock-master-api/blob/master/src/Training.WebApi/Controllers/CustomersController.cs"
    click P2 "https://github.com/mayorgacdev/stock-master-api/blob/master/src/Training.WebApi/Controllers/WarehouseController.cs"
    click P3 "https://github.com/mayorgacdev/stock-master-api/blob/master/src/Training.WebApi/Middlewares/TrainingMiddleware.cs"
    click P4 "https://github.com/mayorgacdev/stock-master-api/blob/master/src/Training.WebApi/appsettings.json"
    click P5 "https://github.com/mayorgacdev/stock-master-api/blob/master/src/Training.WebApi/Program.cs"

    %% Click Events for Application Layer
    click A1 "https://github.com/mayorgacdev/stock-master-api/blob/master/src/Training.Application/Services/CustomerService.cs"
    click A2 "https://github.com/mayorgacdev/stock-master-api/blob/master/src/Training.Application/Services/WarehouseService.cs"
    click A3 "https://github.com/mayorgacdev/stock-master-api/blob/master/src/Training.Application/Mapping/MappingProfile.cs"
    click A4 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Application/Requests"
    click A5 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Application/Validations"
    click A6 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Application/Attributes"
    click A7 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Application/Extensions"

    %% Click Events for Domain Layer
    click D1 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Domain/Inventory"
    click D2 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Domain/Sales"
    click D3 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Domain/Common"

    %% Click Events for Infrastructure Layer
    click I1 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Infraestructure/Data"
    click I2 "https://github.com/mayorgacdev/stock-master-api/blob/master/src/Training.Infraestructure/TrainingDbContext.cs"
    click I3 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Infraestructure/Migrations"
    click I4 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Infraestructure/Interfaces"
    click I5 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Infraestructure/Inventory"
    click I6 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Infraestructure/Sales"

    %% Click Events for Common Layer
    click C1 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Common/Extensions"
    click C2 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Common/Responses"
    click C3 "https://github.com/mayorgacdev/stock-master-api/tree/master/src/Training.Common/ServicesFilter"

    %% Styling Definitions
    classDef client fill:#99B898,stroke:#333,stroke-width:2px;
    classDef presentation fill:#F8B195,stroke:#333,stroke-width:2px;
    classDef application fill:#F67280,stroke:#333,stroke-width:2px;
    classDef domain fill:#C06C84,stroke:#333,stroke-width:2px;
    classDef infrastructure fill:#6C5B7B,stroke:#333,stroke-width:2px;
    classDef common fill:#355C7D,stroke:#fff,stroke-width:2px,color:#fff;
```
