# krlosoul/netcoreapitemplate architecture

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

- Interactive diagram: https://gitdiagram.com/krlosoul/netcoreapitemplate
- Repository: https://github.com/krlosoul/netcoreapitemplate

## Overview

##### Main Components and Layers
- API Layer (Presentation): Represented by the “Api” directory. This component exposes HTTP endpoints, has controllers for users and roles, applies filters, middlewares for exception handling, versioning, Swagger configuration, and authentication (JWT and Keycloak).
- Application Layer (CQRS + Mediator): Represented by the “Application” directory. This layer contains features organized by domain (Roles and Users) and implements commands, queries, DTOs, mappers, validators and behaviours to enforce validations.
- Core (Domain Layer): Shows the business entities (User, Role, UserRole), core constants, shared DTOs, exceptions, enums, and interfaces for abstractions (e.g., repository, unit of work, JWT service, etc.). This layer defines the core business logic and contracts.
- Infrastructure Layer: Contains implementation details such as data access (Entity configurations, Repository, UnitOfWork), details of connecting to the SQL Server DB, and implementations of external services (Kafka, Redis, RabbitMQ, Hangfire, MinIO, etc.). It also contains dependency injection configuration.
- Database: SQL Server. Represented by the DDL provided in the README. The Infrastructure layer interfaces with the database using Repository and UnitOfWork.
- External Integrations: Include services such as Vault for secret management, MinIO for file storage, ElasticSearch/Kibana (via ELK) for logging, Kafka & RabbitMQ for messaging, Keycloak for authentication/authorization, Alfresco for document management, Redis for caching, Hangfire for background jobs, and circuit breakers, among others.

##### Relationships and Data Flow
- That user interactions (HTTP Requests) enter through the API layer.
- From API, the flow continues to the Application layer using the Mediator pattern; commands and queries are issued that encapsulate the business logic.
- The Application layer communicates with the Core for domain validations and business rule enforcement.
- When persistence is needed, the Application layer forwards requests to the Infrastructure layer, which handles Repository/UnitOfWork operations with the SQL Server.
- External services (Vault, MinIO, Kafka, etc.) are invoked through specific service interfaces defined in Core/Application and implemented in Infrastructure.

##### Architectural Patterns and Design Principles
- Hexagonal Architecture (Ports & Adapters): Emphasize the separation between the core business logic (Core) and the outer layers (API, Infrastructure).
- CQRS (Command Query Responsibility Segregation): Indicate distinct flows for commands (state changes) and queries (data retrieval).
- Mediator Pattern: Show a central component (the mediator) coordinating request handling in the Application layer.
- Dependency Injection and Abstraction: The Application and Core layers define interfaces which are implemented in the Infrastructure layer.
- Containerization: Docker and Docker Compose are used to deploy the API and its external integrations; represent these containers in a separate “Deployment/Container” cluster.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% API Layer (Presentation)
    subgraph "API Layer (Presentation)"
        apiControllers["Controllers<br/>(Exposes HTTP Endpoints)"]:::api
        apiMiddlewares["Middlewares<br/>(Exception Handling, Authentication)"]:::api
        apiConfigurations["Configurations<br/>(Swagger, Versioning)"]:::api
    end

    %% Application Layer (CQRS + Mediator)
    subgraph "Application Layer (CQRS & Mediator)"
        appFeatures["Features<br/>(Commands/Queries & Domain Features)"]:::app
        appBehaviours["Behaviours<br/>(Validation, Pipelines)"]:::app
        appDI["Dependency Injection<br/>(Application DI)"]:::app
        mediator["Mediator<br/>(Request Coordination)"]:::app
    end

    %% Core Domain
    subgraph "Core Domain"
        coreEntities["Entities"]:::core
        coreExceptions["Exceptions"]:::core
        coreInterfaces["Interfaces"]:::core
        coreDTOs["DTOs"]:::core
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        infraDataAccess["Data Access<br/>(Repository & UnitOfWork)"]:::infra
        infraServices["Services<br/>(External Integrations)"]:::infra
        infraDI["Dependency Injection<br/>(Infra DI)"]:::infra
    end

    %% External Integrations
    subgraph "External Integrations"
        kafka["Kafka"]:::external
        rabbitMQ["RabbitMQ"]:::external
        redis["Redis"]:::external
        minio["MinIO"]:::external
        alfresco["Alfresco"]:::external
        keycloak["Keycloak"]:::external
        vault["Vault"]:::external
        elk["ELK"]:::external
        hangfire["Hangfire"]:::external
    end

    %% Database
    subgraph "Database"
        sqlDatabase["SQL Server Database"]:::db
    end

    %% Deployment / Container
    subgraph "Deployment/Container"
        docker["Docker Deployment"]:::deployment
    end

    %% Relationships - Data Flow
    apiControllers -->|"HTTPRequests"| mediator
    apiMiddlewares -->|"HTTPRequests"| mediator
    apiConfigurations -->|"APIConfig"| mediator

    mediator -->|"Executes"| appFeatures
    mediator -->|"Executes"| appBehaviours

    appFeatures -->|"Validates"&nbsp;| coreEntities
    appBehaviours -->|"Validates"&nbsp;| coreExceptions
    appDI -->|"Injects"| infraDI

    mediator -->|"Delegates"| infraDataAccess
    mediator -->|"Delegates"| infraServices

    infraDataAccess -->|"Persists Data"| sqlDatabase

    infraServices -->|"Integrates"| kafka
    infraServices -->|"Integrates"| rabbitMQ
    infraServices -->|"Integrates"| redis
    infraServices -->|"Integrates"| minio
    infraServices -->|"Integrates"| alfresco
    infraServices -->|"Integrates"| keycloak
    infraServices -->|"Integrates"| vault
    infraServices -->|"Integrates"| elk
    infraServices -->|"Integrates"| hangfire

    docker --> apiControllers
    docker --> appFeatures
    docker --> infraDataAccess

    %% Click Events for API Layer
    click apiControllers "https://github.com/krlosoul/netcoreapitemplate/tree/main/Api/Controllers"
    click apiMiddlewares "https://github.com/krlosoul/netcoreapitemplate/tree/main/Api/Middlewares"
    click apiConfigurations "https://github.com/krlosoul/netcoreapitemplate/tree/main/Api/Configurations"

    %% Click Events for Application Layer
    click appFeatures "https://github.com/krlosoul/netcoreapitemplate/tree/main/Application/Features"
    click appBehaviours "https://github.com/krlosoul/netcoreapitemplate/tree/main/Application/Behaviours"
    click appDI "https://github.com/krlosoul/netcoreapitemplate/blob/main/Application/DependencyInjection.cs"

    %% Click Events for Core Domain
    click coreEntities "https://github.com/krlosoul/netcoreapitemplate/tree/main/Core/Entities"
    click coreExceptions "https://github.com/krlosoul/netcoreapitemplate/tree/main/Core/Exceptions"
    click coreInterfaces "https://github.com/krlosoul/netcoreapitemplate/tree/main/Core/Interfaces"
    click coreDTOs "https://github.com/krlosoul/netcoreapitemplate/tree/main/Core/Dtos"

    %% Click Events for Infrastructure Layer
    click infraDataAccess "https://github.com/krlosoul/netcoreapitemplate/tree/main/Infrastructure/DataAccess"
    click infraServices "https://github.com/krlosoul/netcoreapitemplate/tree/main/Infrastructure/Services"
    click infraDI "https://github.com/krlosoul/netcoreapitemplate/blob/main/Infrastructure/DependencyInjection.cs"

    %% Click Events for External Integrations
    click kafka "https://github.com/krlosoul/netcoreapitemplate/blob/main/Infrastructure/Services/KafkaProducerService.cs"
    click minio "https://github.com/krlosoul/netcoreapitemplate/blob/main/Infrastructure/Services/MinioService.cs"
    click redis "https://github.com/krlosoul/netcoreapitemplate/blob/main/Infrastructure/Services/RedisCacheService.cs"

    %% Click Event for Deployment
    click docker "https://github.com/krlosoul/netcoreapitemplate/blob/main/.dockerignore"

    %% Styles
    classDef api fill:#add8e6,stroke:#00008b,stroke-width:2px;
    classDef app fill:#c1e1c1,stroke:#2e8b57,stroke-width:2px;
    classDef core fill:#ffeb99,stroke:#b8860b,stroke-width:2px;
    classDef infra fill:#deb887,stroke:#8b4513,stroke-width:2px;
    classDef external fill:#d3d3d3,stroke:#696969,stroke-dasharray: 5 5,stroke-width:2px;
    classDef db fill:#ffe4e1,stroke:#ff4500,stroke-dasharray: 2 2,stroke-width:2px;
    classDef deployment fill:#b0e0e6,stroke:#4682b4,stroke-width:2px;
```
