# dev-lusaja/fastapi-hexagonal architecture

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

- Interactive diagram: https://gitdiagram.com/dev-lusaja/fastapi-hexagonal
- Repository: https://github.com/dev-lusaja/fastapi-hexagonal

## Overview

##### Main Components
- Domain Layer:
– Contains the business core with entities (app/domain/entities), events (app/domain/events), repositories interfaces (app/domain/repositories), and use cases (app/domain/use_cases) along with domain-specific exceptions (app/domain/exceptions.py).
- Application Layer:
– Responsible for orchestrating use cases and validations. It includes service implementations (app/application/services) and input data validations (app/application/validators).
- Infrastructure Layer:
– Provides concrete implementations and external integrations. This includes API handlers (app/infrastructure/handlers), repository implementations (app/infrastructure/repositories), event adapters (app/infrastructure/events), data schemas (app/infrastructure/schemas), and the dependency injection container (app/infrastructure/container.py).
- Test Layer:
– Contains tests for services, API endpoints, and test data (under app/test).

##### Relationships and Data Flow
- Domain is at the center and defines all core business logic and interfaces (or “ports”).
- The Application layer sits next to the Domain and implements the use cases by calling into domain logic, leveraging input validators.
- The Infrastructure layer encircles both Domain and Application as “adapters” that provide actual I/O operations (e.g., web APIs via FastAPI in app/infrastructure/fast_api.py, repository implementations, etc.).
- The Test layer interacts with all layers by verifying that the behavior adheres to expectations.

##### Architectural Patterns and Design Principles
- Visually distinguish the Domain (the inner hexagon/core) from the Application layer (which implements business use cases) and the outer Infrastructure layer (adapters performing I/O).
- That Domain should be independent – it contains no third-party libraries, while Infrastructure integrates with frameworks like FastAPI, dependency-injector, and pydantic.
- Dependency inversion where the Application and Infrastructure layers depend on Domain interfaces rather than the other way around.

##### Relevant Technologies
- Python is the main language.
- FastAPI serves as the web framework (located in app/infrastructure/fast_api.py and app/infrastructure/handlers).
- dependency-injector is used for dependency management (app/infrastructure/container.py).
- pytest is used for testing (test files under app/test).
- Docker and Make are used for containerization and build orchestration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Domain Layer
    subgraph "Domain Layer"
        D1("Entities")
        D2("Events")
        D3("Repository Interfaces")
        D4("Use Cases")
        D5("Exceptions")
    end

    %% Application Layer
    subgraph "Application Layer"
        A1("Services")
        A2("Validators")
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        I1("API Handlers")
        I2("Repository Implementations")
        I3("Event Adapters")
        I4("Data Schemas")
        I5("Dependency Injection Container")
        I6("FastAPI Integration")
    end

    %% Test Layer
    subgraph "Test Layer"
        T1("Tests for API Endpoints")
        T2("Tests for Services")
        T3("Test Data")
    end

    %% Relationships among Layers
    A1 -->|"calls"| D4
    A2 -->|"validates"| D4

    I1 -->|"invokes"| A1
    I2 -->|"implements"| D3
    I3 -->|"adapts"| D2
    I5 -->|"injects"| A1
    I6 -->|"integrates"| I1

    T1 -->|"tests"| I1
    T2 -->|"tests"| A1
    T3 -->|"provides"| T2

    %% Class Definitions for Colours
    classDef domain fill:#FFCDD2,stroke:#B71C1C,stroke-width:2px;
    classDef application fill:#BBDEFB,stroke:#0D47A1,stroke-width:2px;
    classDef infrastructure fill:#C8E6C9,stroke:#1B5E20,stroke-width:2px;
    classDef test fill:#FFECB3,stroke:#F57F17,stroke-width:2px;

    class D1,D2,D3,D4,D5 domain;
    class A1,A2 application;
    class I1,I2,I3,I4,I5,I6 infrastructure;
    class T1,T2,T3 test;

    %% Click Events for Domain Layer
    click D1 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/domain/entities/product.py"
    click D2 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/domain/events/product.py"
    click D3 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/domain/repositories/product.py"
    click D4 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/domain/use_cases/product.py"
    click D5 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/domain/exceptions.py"

    %% Click Events for Application Layer
    click A1 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/application/services/product.py"
    click A2 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/application/validators/product.py"

    %% Click Events for Infrastructure Layer
    click I1 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/infrastructure/handlers/products.py"
    click I2 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/infrastructure/repositories/product.py"
    click I3 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/infrastructure/events/product.py"
    click I4 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/infrastructure/schemas/product.py"
    click I5 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/infrastructure/container.py"
    click I6 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/infrastructure/fast_api.py"

    %% Click Events for Test Layer
    click T1 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/test/fastapi/test_product_api.py"
    click T2 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/test/services/test_product.py"
    click T3 "https://github.com/dev-lusaja/fastapi-hexagonal/blob/main/app/test/data/product.py"
```
