# rodri2d2/mcarickandmorty architecture

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

- Interactive diagram: https://gitdiagram.com/rodri2d2/mcarickandmorty
- Repository: https://github.com/rodri2d2/mcarickandmorty

## Overview

##### Project Type and Purpose
- This is an iOS client app that implements a front-end user interface using SwiftUI.
- It follows the MVVM pattern, clearly separating Views, ViewModels, and Models with additional layers (Application, Data, Domain, and Configuration) for handling business logic, API calls, and dependency injection.
- The README emphasizes modern asynchronous programming (using async/await), custom cache implementations, and TDD methodology.

##### File Structure for Architectural Clues
- Top-level folders include a primary source folder “MCARickAndMorty” that contains several subdirectories:
- Application: Contains Repositories, UseCases (with further separation for character-specific use cases), and Validators. This layer represents the business logic and data flow management.
- Configuration: Contains DependencyContainer and the main application entry point (MCARickAndMortyApp.swift). This highlights the use of dependency injection.
- Data: Contains the DataManager (for characters and episodes), and a well-organized Network subdirectory. The Network subdirectory is subdivided into Connection, Paths, Requests, and Service. This captures the API consuming mechanism.
- Domain: Contains models (such as CharacterInfoResponse and EpisodeInfoResponse) that serve as the data contracts between layers.
- Presentation: Contains screens and view models for characters (e.g., CharactersView, CharactersViewModel, and CharacterDetailView) and common UIComponents.
- Tests: Mirrors the structure of the main project to support unit testing in each layer.
- The careful organization of directories indicates adherence to separation of concerns, a hallmark of the MVVM approach.

- Main Components:
- Frontend (Presentation) Layer: SwiftUI Views and UIComponents, along with associated ViewModels (e.g., CharactersViewModel).
- Application/Business Logic Layer: UseCases (such as FetchCharactersUseCase and FetchSingleCharacterUseCase), Validators (for data integrity), and Repositories (that abstract data retrieval details).
- Data Layer: DataManagers and the Network module (comprising Connection configuration, API paths, various API requests, and the NetworkService) that handle low-level API communication.
- Domain Layer: Models representing characters and episodes that are used consistently across the application.
- Configuration & Dependency Injection: DependencyContainer classes that wire together the various components, ensuring decoupling and testability.
- Tests: Although not part of the live system, the test suites reflect the intended modularity and can be denoted to show TDD compliance.

- Relationships & Flow:
- The Presentation layer (SwiftUI views and view models) interacts with the Application layer via use cases.
- The Application layer coordinates with the Data layer through Repositories and DataManagers for API interactions.
- The Data layer communicates with external services (Rick and Morty API) using network components (Connections, Requests, and Services).
- The Domain layer uniformly represents data structures across these interactions.
- Dependency injection (in the Configuration layer) ensures that ViewModels, UseCases, and DataManagers are instantiated with the correct dependencies.

##### Architectural Patterns and Design Principles to Highlight
- MVVM: Clearly distinguish between Views, ViewModels, and Models.
- Dependency Injection: Emphasize the use of DependencyContainers for assembling components.
- Asynchronous Programming: Note the use of async/await in API calls and data handling.
- Test Driven Development (TDD): Show supporting test suites mirroring production components.
- Separation of Concerns: Each layer is designed to handle specific responsibilities – presentation, business logic, data, etc.

##### Final Advice
- On how asynchronous operations and custom caching might impact the design; denote these in the Data/Network section.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    U1["User"]:::external

    %% Presentation Layer
    subgraph "Presentation Layer"
        P1["CharactersView"]:::presentation
        P2["CharactersViewModel"]:::presentation
        P3["CharacterDetailView"]:::presentation
    end

    %% Application/Business Logic Layer
    subgraph "Application/Business Logic Layer"
        subgraph "UseCases"
            A1["FetchCharactersUseCase"]:::application
            A2["FetchSingleCharacterUseCase"]:::application
        end
        A3["CharacterRepository"]:::application
        subgraph "Validators"
            A4["GreaterThanZeroValidator"]:::application
            A5["Validator"]:::application
            A6["ValidationErrors"]:::application
        end
    end

    %% Data Layer
    subgraph "Data Layer"
        subgraph "Data Managers"
            D1["CharacterDataManager"]:::data
            D2["EpisodesDataManager"]:::data
            D3["DataManager"]:::data
        end
        subgraph "Network Module"
            N1["Connection"]:::data
            N2["Paths"]:::data
            N3["Requests"]:::data
            N4["Service"]:::data
        end
    end

    %% Domain Models
    subgraph "Domain Models"
        DM1["CharacterInfoResponse"]:::domain
        DM2["EpisodeInfoResponse"]:::domain
    end

    %% Configuration & Dependency Injection
    subgraph "Configuration & Dependency Injection"
        C1["DependencyContainer"]:::config
        C2["CharactersDependencyContainer"]:::config
        C3["DependencyContainerProtocol"]:::config
        C4["MCARickAndMortyApp"]:::config
    end

    %% Test Modules
    subgraph "Test Modules"
        T1["CharacterDataManagerTests"]:::tests
        T2["APIErrorTests"]:::tests
    end

    %% External API
    RMA(("Rick and Morty API")):::external

    %% Relationships and Data Flow
    U1 -->|"taps"| P1
    P1 -->|"bindsTo"| P2
    P2 -->|"calls"| A1
    P2 -->|"calls"| A2
    A1 -->|"invokes"| A3
    A2 -->|"invokes"| A3
    A1 -->|"validatesWith"| A4
    A1 -->|"validatesWith"| A5
    A1 -->|"validatesWith"| A6
    A2 -->|"validatesWith"| A4
    A2 -->|"validatesWith"| A5
    A2 -->|"validatesWith"| A6
    A3 -->|"mapsTo"| DM1
    A3 -->|"mapsTo"| DM2
    A3 -->|"queries"| D1
    D1 -->|"fetchesDataVia"| N4
    N4 -->|"calls"| RMA
    D1 -->|"returns"| DM1
    D1 -->|"returns"| DM2

    %% Dependency Injection Connections
    C1 -->|"injects"| P2
    C1 -->|"injects"| A1
    C1 -->|"injects"| A2
    C1 -->|"injects"| A3
    C1 -->|"injects"| D1

    %% Test Modules Associations
    T1 -->|"tests"| D1
    T2 -->|"tests"| N4

    %% Styling Classes
    classDef presentation fill:#FFEB3B,stroke:#333,stroke-width:2px
    classDef application fill:#90CAF9,stroke:#333,stroke-width:2px
    classDef data fill:#A5D6A7,stroke:#333,stroke-width:2px
    classDef domain fill:#FFCC80,stroke:#333,stroke-width:2px
    classDef config fill:#B3E5FC,stroke:#333,stroke-width:2px
    classDef tests fill:#FFAB91,stroke:#333,stroke-width:2px
    classDef external fill:#C5CAE9,stroke:#333,stroke-width:2px

    %% Click Events for Presentation Layer
    click P1 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Presentation/Characters/CharactersView.swift"
    click P2 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Presentation/Characters/CharactersViewModel.swift"
    click P3 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Presentation/Characters/CharacterDetailView.swift"

    %% Click Events for Application/Business Logic Layer - UseCases and Validators
    click A1 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Application/UseCases/CharactersUseCases/FetchCharactersUseCase.swift"
    click A2 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Application/UseCases/CharactersUseCases/FetchSingleCharacterUseCase.swift"
    click A3 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Application/Repositories/CharacterRepository.swift"
    click A4 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Application/Validators/GreaterThanZeroValidator.swift"
    click A5 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Application/Validators/Validator.swift"
    click A6 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Application/Validators/ValidationErrors.swift"

    %% Click Events for Data Layer - Data Managers and Network Module
    click D1 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Data/DataManager/CharacterDataManager.swift"
    click D2 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Data/DataManager/EpisodesDataManager.swift"
    click D3 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Data/DataManager/DataManager.swift"
    click N1 "https://github.com/rodri2d2/mcarickandmorty/tree/main/MCARickAndMorty/MCARickAndMorty/Data/Network/Connection"
    click N2 "https://github.com/rodri2d2/mcarickandmorty/tree/main/MCARickAndMorty/MCARickAndMorty/Data/Network/Paths"
    click N3 "https://github.com/rodri2d2/mcarickandmorty/tree/main/MCARickAndMorty/MCARickAndMorty/Data/Network/Requests"
    click N4 "https://github.com/rodri2d2/mcarickandmorty/tree/main/MCARickAndMorty/MCARickAndMorty/Data/Network/Service"

    %% Click Events for Domain Models
    click DM1 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Domain/Models/CharacterInfoResponse.swift"
    click DM2 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Domain/Models/EpisodeInfoResponse.swift"

    %% Click Events for Configuration & Dependency Injection
    click C1 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Configuration/DependencyContainer/DependencyContainer.swift"
    click C2 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Configuration/DependencyContainer/CharactersDependencyContainer.swift"
    click C3 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Configuration/DependencyContainer/DependencyContainerProtocol.swift"
    click C4 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMorty/Configuration/MCARickAndMortyApp.swift"

    %% Click Events for Test Modules
    click T1 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMortyTests/CharacterDataManagerTests.swift"
    click T2 "https://github.com/rodri2d2/mcarickandmorty/blob/main/MCARickAndMorty/MCARickAndMortyTests/APIErrorTests.swift"
```
