# redline-solutions-inc/redline-gaia architecture

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

- Interactive diagram: https://gitdiagram.com/redline-solutions-inc/redline-gaia
- Repository: https://github.com/redline-solutions-inc/redline-gaia

## Overview

##### Project type and purpose
- This is a full-stack .NET API project following Clean (Onion) Architecture principles, implementing a multi-tenant architecture with separate databases per tenant, plus a master database (“Tenant Registry”).
- The solution exposes HTTP endpoints via ASP.NET Core Web API, processes commands/queries via MediatR, persists using Entity Framework, and supports tenant migrations via a console app.

##### File structure
- Top-level projects/layers:
- RedLine-Gaia.Domain – core business entities, errors, repository and service interfaces.
- RedLine-Gaia.Application – application layer with features (Commands, Queries, DTOs), MediatR handlers, validation behaviors, Mapster config, DI.
- RedLine-Gaia.Infrastructure – EF Core DbContexts (MasterDbContext, ApplicationDbContext for tenant), repositories, Unit of Work, tenant/current-tenant services, migrations folders.
- RedLine-Gaia.WebApi – ASP.NET Core API: controllers, middleware (GlobalExceptionHandler, TenantResolver, TenantActionFilter), Program.cs, appsettings.
- TenantDatabaseMigrater – console app to run EF migrations against master and tenant DBs.
- Tests – xUnit tests for application and infrastructure layers.

##### README insights
- Emphasizes Onion Architecture: Domain at center, outer layers depend inward.
- Multi-tenant design: shared schema per tenant DB, plus master DB for registry.
- Core patterns: Repository, Unit of Work, MediatR, FluentValidation, Mapster.
- External dependencies: Serilog/BetterStack logging, Scalar (Swagger UI generation).

a. Main components:
- Presentation/API Layer (WebApi project, Controllers, Middleware)
- Application Layer (MediatR pipeline: Commands, Queries, Handlers, Behaviors, DTOs)
- Domain Layer (Entities, Errors, Interfaces)
- Infrastructure Layer
– Tenant Registry (MasterDbContext + TenantMaster migrations)
– Tenant Databases (ApplicationDbContext + Migrations directory)
– Repositories & UoW
– CurrentTenantService, TenantService, DbContextFactory
- TenantDatabaseMigrater (Console app orchestrating migrations)
- External Systems: BetterStack logging, SQL Servers for Master + Tenant DBs, Swagger/Scalar UI

b. Relationships & interactions:
- Client HTTP → WebApi Middleware (TenantResolver reads header, sets current tenant)
→ Controller → MediatR → Application Handlers → inject DbContext from DbContextFactory → Repositories/UoW → EF Core → Tenant DB → return ResultDTO → back up the chain.
- TenantService (singleton) → reads master DB → resolves tenant connection string.
- CurrentTenantService (scoped) → holds tenant info for each request.
- TenantDatabaseMigrater console → uses TenantService → enumerates tenants → applies EF migrations to each tenant DB (and Master DB).

c. Architectural patterns & design principles:
- Onion/Clean Architecture: clear layer dependencies inward.
- CQRS via MediatR: separating reads (Queries) and writes (Commands).
- Repository + Unit of Work for data access abstraction.
- Dependency Injection across layers (Infrastructure implements Domain interfaces).
- Middleware for cross-cutting concerns: exception handling, tenant resolution.
- Multi-tenancy via separate databases with a registry pattern.

d. Key technologies/frameworks:
- ASP.NET Core Web API, Middleware pipeline
- MediatR for in-process messaging
- Entity Framework Core for ORM
- FluentValidation for command input validation
- Mapster for DTO mapping
- Serilog + BetterStack for logging
- Scalar for API documentation UI
- xUnit + FluentAssertions + NSubstitute for unit tests

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Clients
    client["HTTP Client"]:::ext
    client --> middleware2

    subgraph "Presentation Layer"
        middleware1["GlobalExceptionHandlerMiddleware"]:::api
        middleware2["TenantResolver"]:::api
        middleware3["TenantActionFilter"]:::api
        controller["ProductController"]:::api
        configP1["Program.cs"]:::api
        configP2["appsettings.json"]:::api
        configP3["appsettings.Development.json"]:::api
    end

    subgraph "Application Layer"
        diApp["DI Setup"]:::app
        mapster["MapsterConfig"]:::app
        validation["ValidationBehavior"]:::app
        features["Features\nCommands/Queries/Handlers/DTOs"]:::app
        dtoUtil["ResultDTO Utilities"]:::app
    end

    subgraph "Domain Layer"
        domainEntities1["Tenant"]:::domain
        domainEntities2["Product"]:::domain
        errors["Errors"]:::domain
        interfaces["Interfaces"]:::domain
    end

    subgraph "Infrastructure Layer"
        masterCtx["MasterDbContext\n(Tenant Registry)"]:::infra
        tenantCtx["ApplicationDbContext\n(Tenant DB)"]:::infra
        migrations1["Tenant DB Migrations"]:::infra
        migrations2["Master DB Migrations"]:::infra
        repo["ProductRepository"]:::infra
        uow["UnitOfWork"]:::infra
        svc1["CurrentTenantService"]:::infra
        svc2["TenantService"]:::infra
        svc3["DbContextFactory"]:::infra
        diInfra["DI Setup"]:::infra
    end

    subgraph "TenantDatabaseMigrater"
        migrater["Program & Config"]:::console
    end

    subgraph "External Systems"
        logging["BetterStack / Serilog"]:::ext
        swagger["Scalar (Swagger UI)"]:::ext
        dbs["SQL Servers\nMaster & Tenant DBs"]:::ext
    end

    %% Presentation → Application
    middleware1 --> controller
    middleware2 --> controller
    middleware3 --> controller
    configP1 --> controller
    configP2 --> controller
    configP3 --> controller

    controller --> features
    features --> validation
    features --> mapster
    features --> diApp
    diApp --> interfaces
    features --> dtoUtil

    %% Application → Domain/Infrastructure
    features --> repo
    repo --> uow
    uow --> tenantCtx
    svc1 --> tenantCtx
    svc2 --> masterCtx
    svc3 --> tenantCtx

    middleware2 --> svc1

    %% Infrastructure → External DBs
    masterCtx --> dbs
    tenantCtx --> dbs

    %% Logging & Documentation
    controller --> logging
    features --> logging
    swagger --> controller

    %% Migrater interactions
    migrater --> svc2
    migrater --> migrations2
    migrater --> migrations1
    migrations2 --> masterCtx
    migrations1 --> tenantCtx

    %% Click Events
    click middleware1 "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.WebApi/Middleware/GlobalExceptionHandlerMiddleware.cs"
    click middleware2 "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.WebApi/Middleware/TenantResolver.cs"
    click middleware3 "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.WebApi/Middleware/TenantActionFilter.cs"
    click controller "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.WebApi/Controllers/ProductController.cs"
    click configP1 "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.WebApi/Program.cs"
    click configP2 "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.WebApi/appsettings.json"
    click configP3 "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.WebApi/appsettings.Development.json"

    click diApp "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Application/DependencyInjection.cs"
    click mapster "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Application/Configuration/MapsterConfig.cs"
    click validation "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Application/Behaviors/ValidationBehavior.cs"
    click features "https://github.com/redline-solutions-inc/redline-gaia/tree/main/RedLine-Gaia.Application/Features/Products"
    click dtoUtil "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Application/ResultDto/ResultDTO.cs"

    click domainEntities1 "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Domain/Entities/Master/Tenant.cs"
    click domainEntities2 "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Domain/Entities/Product.cs"
    click errors "https://github.com/redline-solutions-inc/redline-gaia/tree/main/RedLine-Gaia.Domain/Errors"
    click interfaces "https://github.com/redline-solutions-inc/redline-gaia/tree/main/RedLine-Gaia.Domain/Interfaces"

    click masterCtx "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Infrastructure/Database/MasterDbContext.cs"
    click tenantCtx "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Infrastructure/Database/ApplicationDbContext.cs"
    click migrations1 "https://github.com/redline-solutions-inc/redline-gaia/tree/main/RedLine-Gaia.Infrastructure/Migrations"
    click migrations2 "https://github.com/redline-solutions-inc/redline-gaia/tree/main/RedLine-Gaia.Infrastructure/Migrations/TenantMaster"
    click repo "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Infrastructure/Repositories/ProductRepository.cs"
    click uow "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Infrastructure/UoW/UnitOfWork.cs"
    click svc1 "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Infrastructure/Services/CurrentTenantService.cs"
    click svc2 "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Infrastructure/Services/TenantService.cs"
    click svc3 "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Infrastructure/Services/DbContextFactory.cs"
    click diInfra "https://github.com/redline-solutions-inc/redline-gaia/blob/main/RedLine-Gaia.Infrastructure/DependencyInjection.cs"

    click migrater "https://github.com/redline-solutions-inc/redline-gaia/blob/main/TenantDatabaseMigrater/Program.cs"

    %% Styles
    classDef api fill:#D6EAF8,stroke:#1B4F72,color:#1B2631;
    classDef app fill:#D5F5E3,stroke:#196F3D,color:#145A32;
    classDef domain fill:#FCF3CF,stroke:#7D6608,color:#515A5A;
    classDef infra fill:#FDEBD0,stroke:#BA4A00,color:#78281F;
    classDef console fill:#E8DAEF,stroke:#7D3C98,color:#4A235A;
    classDef ext fill:#F5B7B1,stroke:#922B21,color:#641E16;
```
