# jgmelon22/externalapiconsumer architecture

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

- Interactive diagram: https://gitdiagram.com/jgmelon22/externalapiconsumer
- Repository: https://github.com/jgmelon22/externalapiconsumer

## Overview

##### Project Type and Purpose
- This is an ASP.NET Core Web API project built as a weekend experiment, whose main aim is to consume an external API (built in Java/Spring and hosted on Render).
- The project uses Refit to make type-safe HTTP calls, along with MediatR for handling commands/queries and Swagger for API documentation.
- It is structured using a layered (or onion/Clean Architecture-like) approach, separating API, Application, Core (Domain), and Infrastructure concerns.

##### Key Layers and Components
a. API Layer (Presentation):
- Located in “src/ExternalApiConsumer.API”.
- Contains Controllers (e.g., PeopleController.cs) and Program.cs (the application startup).
- Exposes endpoints which the client (or Swagger UI) interacts with.
b. Application Layer (Business Logic):
- Located in “src/ExternalApiConsumer.Application”.
- Contains Commands (e.g., CreatePersonCommand, DeletePersonCommand, SeedDataCommand) and Queries (e.g., GetPeopleQuery, GetPersonByIdQuery) along with their respective Handlers.
- Implements the Mediator pattern (via MediatR) to decouple handling logic.
c. Core/Domain Layer:
- Located in “src/ExternalApiConsumer.Core”.
- Defines the domain entities (e.g., Person), Data Transfer Objects (DTOs) for requests/responses, and mapping extensions.
- Encapsulates essential business rules and common/shared services.
d. Infrastructure Layer:
- Located in “src/ExternalApiConsumer.Infrastructure”.
- Contains interfaces and concrete service implementations for interacting with external systems – notably with the external API through the IExternalPersonApi interface (using Refit) and the IManagePersonService from which ManagePersonService is implemented.
- Acts as the bridge between the internal domain and external services.
e. Testing:
- The “tests” folder contains distinct test projects for the API, Application, and Infrastructure layers ensuring each component is validated.

##### Relationships and Interactions
- The API Layer (controller endpoints) receives client HTTP requests and delegates work to the Application Layer by sending commands or queries (typically via MediatR).
- The Application Layer uses Domain objects (from the Core layer) for business logic and validation.
- When external data is needed, the Application Layer calls into the Infrastructure Layer (via defined interfaces) where services like ManagePersonService use Refit to consume the external API.
- Data returned from the Infrastructure is then mapped back (using Mapping Extensions in Core) to DTOs which the API layer returns as HTTP responses.
- Incoming HTTP requests into the API Controller.
- The control flow from API → Application (commands/queries via MediatR).
- Application layer invoking Domain logic (business rules, validations).
- Application/Domain calling into Infrastructure to interact with external services.
- Data flow back through these layers before returning a response to the client.

##### Architectural Patterns & Design Principles
- Layered Architecture: separation into API, Application, Domain/Core, and Infrastructure.
- Command and Query Separation via MediatR, ensuring that business logic is encapsulated in handlers.
- Dependency Inversion: the Application and Domain layers rely on abstractions (interfaces defined in Infrastructure), and the concrete implementation is provided in the Infrastructure project.
- External API Consumption via Refit, which abstracts REST API calls in a type-safe way.
- Extensive Use of Dependency Injection (as standard in ASP.NET Core) to wire up these layers.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Nodes
    Client["Client/Swagger"]:::external

    %% API Layer
    subgraph "API Layer"
        Prog["Program.cs"]:::api
        PC["PeopleController"]:::api
    end

    %% Application Layer
    subgraph "Application Layer"
        Handlers["Commands/Queries Handlers"]:::app
    end

    %% Core/Domain Layer
    subgraph "Core/Domain Layer"
        Person["Person Entity"]:::domain
        Mapping["Mapping Extensions"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        APIInterface["IExternalPersonApi"]:::infra
        Service["ManagePersonService"]:::infra
    end

    %% External API
    ExternalAPI["Java/Spring External API (Render)"]:::external

    %% Testing
    Tests["Test Projects"]:::test

    %% Connections
    Client -->|"HTTP_Request"| PC
    PC -->|"MediatR_Dispatch"| Handlers
    Handlers -->|"BusinessLogic"| Person
    Handlers -->|"Mapping"| Mapping
    Handlers -->|"ServiceInvocation"| Service
    Service -->|"Uses"| APIInterface
    APIInterface -->|"RefitCall"| ExternalAPI

    %% Optional back flow (responses)
    ExternalAPI -->|"Response"| APIInterface
    APIInterface -->|"Response"| Service
    Service -->|"Response"| Handlers
    Person -->|"Response"| Handlers
    Mapping -->|"MappedResponse"| Handlers
    Handlers -->|"Response"| PC
    PC -->|"HTTP_Response"| Client

    %% Testing arrows (dashed)
    Tests ---|"Tests_API"| PC
    Tests ---|"Tests_App"| Handlers
    Tests ---|"Tests_Infra"| Service

    %% Click Events
    click Prog "https://github.com/jgmelon22/externalapiconsumer/blob/main/src/ExternalApiConsumer.API/Program.cs"
    click PC "https://github.com/jgmelon22/externalapiconsumer/blob/main/src/ExternalApiConsumer.API/Controllers/PeopleController.cs"
    click Handlers "https://github.com/jgmelon22/externalapiconsumer/blob/main/src/ExternalApiConsumer.Application"
    click Person "https://github.com/jgmelon22/externalapiconsumer/blob/main/src/ExternalApiConsumer.Core/Domains/People/Entities/Person.cs"
    click Mapping "https://github.com/jgmelon22/externalapiconsumer/blob/main/src/ExternalApiConsumer.Core/Domains/People/Mappings/MappingExtensions.cs"
    click APIInterface "https://github.com/jgmelon22/externalapiconsumer/blob/main/src/ExternalApiConsumer.Infrastructure/Interfaces/IExternalPersonApi.cs"
    click Service "https://github.com/jgmelon22/externalapiconsumer/blob/main/src/ExternalApiConsumer.Infrastructure/Services/ManagePersonService.cs"
    click Tests "https://github.com/jgmelon22/externalapiconsumer/tree/main/tests"

    %% Styles
    classDef api fill:#AEDFF7,stroke:#333,stroke-width:2px
    classDef app fill:#FFD6A5,stroke:#333,stroke-width:2px
    classDef domain fill:#C3F0CA,stroke:#333,stroke-width:2px
    classDef infra fill:#E8DAEF,stroke:#333,stroke-width:2px
    classDef external fill:#F7A5A5,stroke:#333,stroke-width:2px
    classDef test fill:#D3D3D3,stroke:#333,stroke-width:2px
```
