# kazuma0606/thread_safe_clean_architechture architecture

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

- Interactive diagram: https://gitdiagram.com/kazuma0606/thread_safe_clean_architechture
- Repository: https://github.com/kazuma0606/thread_safe_clean_architechture

## Overview

##### Main Components
- Domain Layer: Represents the heart of the application containing business rules and entities (e.g., the User entity and the repository interface in src/domain).
- Application Layer: Contains use cases and Data Transfer Objects (DTOs) which process input from the CLI and orchestrate domain operations (located in src/application).
- Adapter Layer: Implements interfaces like repositories and utilities that interact with external systems (for example, the JSON repository in src/adapter and UUID generator in src/adapter/utils).
- Infrastructure/Framework Layer: Provides the CLI interface (src/infrastructure/cli) and the entry point (src/main.rs) that ties everything together.

##### File Structure and Relationships
- The directories (adapter, application, domain, state, and infrastructure) reflect a separation of concerns according to Clean Architecture.
- The adapter layer contains subdirectories for controllers (although labeled “contoroller”) and repository implementations (like json_user_repository).
- The application layer contains DTOs (for both user creation requests and responses) and use case implementations (specifically create_user_usecase).
- The domain layer holds business entities (user) and repository and utility interfaces.
- The infrastructure includes CLI-related modules which serve as the interface for user commands.
- There are configuration files (Cargo.toml, Cargo.lock) that set the Rust environment and dependencies.

##### Incorporate Architectural Patterns and Principles
- Clean Architecture: Emphasize the separation between the inner domain/application layers and the outer adapter/infrastructure layers.
- Dependency Injection: Show how use cases accept repository and id-generator interfaces, making it possible to inject different implementations (mock or production).
- DTO Pattern: Represent the transformation of input data into domain objects and then into output DTOs.
- Thread Safety: Illustrate how shared resource handling is done via Arc<Mutex<>> in the repository for concurrent access.
- Asynchronous Processing: Indicate async operations where non-blocking I/O (e.g., working with JSON files) is used in use cases.

##### Detailing Components and Their Interactions
- Use Case Component: Represent the CreateUserUseCase in the application layer, which receives input DTOs, interacts with the domain by creating User entities, and uses the repository interface to persist data.
- Domain Entities: Show the User entity with its immutable characteristics, populated through domain logic.
- Utility Components: Include an IdGenerator interface in the domain and its UUID generator adapter. Indicate dependency injection by showing the use case receiving both repository and id-generator components through generic parameters.
- Concurrency Aspects: Optionally annotate the JSON repository with a note to highlight the use of Arc<Mutex<>> for thread-safe operations.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Domain Layer
    subgraph "Domain Layer"
        D1["User Entity"]:::domain
        D2["User Repository Interface"]:::domain
        D3["IdGenerator Interface"]:::domain
    end

    %% Application Layer
    subgraph "Application Layer"
        A1["Create User Use Case"]:::application
        A2["User DTOs"]:::application
    end

    %% Adapter Layer
    subgraph "Adapter Layer"
        AD1["JSON User Repository Implementation<br/>(thread-safe: Arc<Mutex<>>)" ]:::adapter
        AD2["UUID Generator Implementation"]:::adapter
        AD3["User Controller"]:::adapter
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        I1["CLI Interface"]:::infrastructure
        I2["Application Entry Point"]:::infrastructure
    end

    %% Connections between layers
    I2 -->|"initiates"| I1
    I1 -->|"sends command"| AD3
    AD3 -->|"transforms request to"| A1
    A1 -->|"creates"| D1
    A1 -->|"calls"| D2
    A1 -->|"calls"| D3
    AD1 -- "implements" --> D2
    AD2 -- "implements" --> D3
    A1 -->|"maps input/output via"| A2

    %% Class definitions for color coding
    classDef domain fill:#F9E79F,stroke:#951b1b,stroke-width:2px;
    classDef application fill:#AED6F1,stroke:#1b4f72,stroke-width:2px;
    classDef adapter fill:#A9DFBF,stroke:#196f3d,stroke-width:2px;
    classDef infrastructure fill:#F5B7B1,stroke:#943126,stroke-width:2px;

    %% Click Events
    click D1 "https://github.com/kazuma0606/thread_safe_clean_architechture/tree/main/src/domain/entity/user"
    click D2 "https://github.com/kazuma0606/thread_safe_clean_architechture/tree/main/src/domain/repository/user_repository_interface"
    click D3 "https://github.com/kazuma0606/thread_safe_clean_architechture/tree/main/src/domain/utils/id_generator_interaface"
    click A1 "https://github.com/kazuma0606/thread_safe_clean_architechture/tree/main/src/application/use_cases/user/create_user_usecase"
    click A2 "https://github.com/kazuma0606/thread_safe_clean_architechture/tree/main/src/application/dto/user"
    click AD1 "https://github.com/kazuma0606/thread_safe_clean_architechture/tree/main/src/adapter/repository/json_user_repository"
    click AD2 "https://github.com/kazuma0606/thread_safe_clean_architechture/tree/main/src/adapter/utils/uuid_generotor"
    click AD3 "https://github.com/kazuma0606/thread_safe_clean_architechture/tree/main/src/adapter/contoroller/user_controller"
    click I1 "https://github.com/kazuma0606/thread_safe_clean_architechture/tree/main/src/infrastructure/cli"
    click I2 "https://github.com/kazuma0606/thread_safe_clean_architechture/blob/main/src/main.rs"
```
