# emanuelmath/xchange architecture

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

- Interactive diagram: https://gitdiagram.com/emanuelmath/xchange
- Repository: https://github.com/emanuelmath/xchange

## Overview

1. This is a layered ASP.NET Core MVC solution with three main projects: `XChange.Core`, `XChange.Infrastructure`, and `XChange.Web`. The architecture cleanly separates domain contracts, data/access implementations, and the presentation layer.

2. `XChange.Core` is the application contract layer. It contains:
   - domain entities like `User` and `User2fa`
   - shared result/error handling via `Common/Result.cs`
   - enums for user/auth/transaction state
   - custom exceptions
   - interfaces for repositories and services such as password hashing, MFA, email, and database connection creation

3. `XChange.Infrastructure` is the implementation layer. It provides concrete infrastructure for the interfaces defined in Core:
   - SQL connection creation in `Data/SqlConnectionFactory.cs`
   - repositories for `User` and `User2fa`
   - `BcryptPasswordHasher` for credential hashing
   - `GoogleMfaService` for MFA/TOTP-based verification

4. `XChange.Web` is the user-facing MVC application. It hosts the HTTP entry point (`Program.cs`), controllers, MVC views, view models, filters, and static assets. This project depends on Core abstractions and Infrastructure implementations through dependency injection.

5. The main user flows are authentication-centric. The presence of `AuthController`, login/register/forgot-password/reset-password/verify-email/verify-mfa views, and related view models indicates a full identity lifecycle:
   registration → email verification → login → optional MFA verification → password recovery/reset.

6. There is also a private authenticated user area. `UserController` plus views like `Dashboard`, `Wallet`, `Transactions`, and `Settings` suggest the app exposes a post-login account section focused on user state and financial/account activity.

7. Access control is enforced at the MVC boundary using filters:
   - `RedirectIfAuthenticatedAttribute` prevents logged-in users from returning to auth pages
   - `RedirectIfNotLoginForMfaAttribute` gates MFA verification so it only works in the correct login flow
   These filters shape request routing before controller actions run.

8. Data access is repository-based, not directly embedded in controllers. Controllers should talk to Core interfaces, which Infrastructure implements with SQL-backed repositories. This gives a clear separation between HTTP handling and persistence concerns.

9. The architecture also includes service abstractions for cross-cutting concerns:
   - password hashing
   - MFA generation/validation
   - email delivery
   These are kept out of the web layer so they can be swapped or mocked independently.

10. The UI layer is Razor-based and split by layout:
   - public pages
   - auth pages
   - private authenticated pages
   Static assets live under `wwwroot`, including Bootstrap, Font Awesome, custom CSS/JS, and a `spanish.json` file that likely supports localized client-side text.

11. From an architecture-diagram perspective, the primary data flow is:
   browser → MVC controller → Core abstractions / view models → Infrastructure repositories & services → SQL database and MFA/email-related services → back to MVC views.

12. The repo appears focused on account and transaction management rather than a generic content site. The combination of user, 2FA, wallet, and transaction concepts implies a fintech or exchange-style application domain, with the core business boundary centered on identity, access, and account state.

Main entry points: [`src/XChange/XChange.Core/Interfaces`](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Core/Interfaces) (Contracts), [`src/XChange/XChange.Web/Controllers/AuthController.cs`](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Controllers/AuthController.cs) (Auth Controller), [`src/XChange/XChange.Web/Program.cs`](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Program.cs) (Program), [`src/XChange/XChange.Web/Controllers/UserController.cs`](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Controllers/UserController.cs) (User Controller)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["XChange.Core"]
  node_core_contracts["Contracts<br/>interfaces<br/>[Interfaces]"]
  node_core_domain["Domain<br/>entities/enums<br/>[Entities]"]
  node_core_result["Result<br/>[Result.cs]"]
  node_core_errors["Errors<br/>exceptions<br/>[Exceptions]"]
end

subgraph group_infra["XChange.Infrastructure"]
  node_infra_sql[("SQL Factory<br/>db access")]
  node_infra_users["User Repos<br/>repositories<br/>[Repositories]"]
  node_infra_services["Security Services<br/>crypto/mfa<br/>[Services]"]
end

subgraph group_web["XChange.Web"]
  node_web_program["Program<br/>host startup<br/>[Program.cs]"]
  node_web_auth["Auth Controller<br/>mvc controller<br/>[AuthController.cs]"]
  node_web_user["User Controller<br/>mvc controller<br/>[UserController.cs]"]
  node_web_home["Home Controller<br/>mvc controller<br/>[HomeController.cs]"]
  node_web_filters["MVC Filters<br/>request guards<br/>[Filters]"]
  node_web_vm_auth["Auth ViewModels<br/>view models<br/>[ViewModels]"]
  node_web_views_auth["Auth Views<br/>razor views<br/>[Auth]"]
  node_web_views_private["Private Views<br/>razor views<br/>[User]"]
  node_web_views_public["Public Views<br/>razor views<br/>[Home]"]
  node_web_layouts["Layouts<br/>razor layout<br/>[Shared]"]
  node_web_static["Static Assets<br/>wwwroot assets<br/>[wwwroot]"]
end

node_web_program -->|"registers"| node_core_contracts
node_web_program -->|"configures"| node_infra_sql
node_web_program -->|"registers"| node_infra_users
node_web_program -->|"registers"| node_infra_services
node_web_auth -->|"uses"| node_core_contracts
node_web_auth -->|"binds"| node_web_vm_auth
node_web_auth -->|"governed by"| node_web_filters
node_web_user -->|"uses"| node_core_contracts
node_web_user -->|"governed by"| node_web_filters
node_web_home -->|"renders"| node_web_layouts
node_web_auth -->|"renders"| node_web_views_auth
node_web_user -->|"renders"| node_web_views_private
node_web_home -->|"renders"| node_web_views_public
node_web_views_auth -->|"uses"| node_web_layouts
node_web_views_private -->|"uses"| node_web_layouts
node_web_views_public -->|"uses"| node_web_layouts
node_web_views_auth -->|"loads"| node_web_static
node_web_views_private -->|"loads"| node_web_static
node_core_contracts -->|"defines"| node_core_domain
node_core_contracts -->|"returns"| node_core_result
node_core_errors -->|"maps to"| node_core_result
node_infra_users -->|"implements"| node_core_contracts
node_infra_services -->|"implements"| node_core_contracts
node_infra_sql -->|"implements"| node_core_contracts
node_infra_users -->|"opens"| node_infra_sql
node_infra_services -->|"works with"| node_core_domain
node_web_auth -->|"handles"| node_core_errors
node_web_user -->|"uses"| node_core_domain

click node_core_contracts "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Core/Interfaces"
click node_core_domain "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Core/Entities"
click node_core_result "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Core/Common/Result.cs"
click node_core_errors "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Core/Exceptions"
click node_infra_sql "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Infrastructure/Data/SqlConnectionFactory.cs"
click node_infra_users "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Infrastructure/Repositories"
click node_infra_services "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Infrastructure/Services"
click node_web_program "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Program.cs"
click node_web_auth "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Controllers/AuthController.cs"
click node_web_user "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Controllers/UserController.cs"
click node_web_home "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Controllers/HomeController.cs"
click node_web_filters "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Filters"
click node_web_vm_auth "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/ViewModels"
click node_web_views_auth "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Views/Auth"
click node_web_views_private "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Views/User"
click node_web_views_public "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Views/Home"
click node_web_layouts "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Views/Shared"
click node_web_static "https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/wwwroot"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_core_contracts,node_core_domain,node_core_result,node_core_errors toneBlue
class node_infra_sql,node_infra_users,node_infra_services toneAmber
class node_web_program,node_web_auth,node_web_user,node_web_home,node_web_filters,node_web_vm_auth,node_web_views_auth,node_web_views_private,node_web_views_public,node_web_layouts,node_web_static toneMint
```

## Components

### XChange.Core

Domain contracts, shared types, and application abstractions

- [Contracts](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Core/Interfaces): interfaces · Repository and service abstractions for database, email, MFA, and hashing · `src/XChange/XChange.Core/Interfaces`
- [Domain](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Core/Entities): entities/enums · User entities plus status and provider constants · `src/XChange/XChange.Core/Entities`
- [Result](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Core/Common/Result.cs): shared result · Common success/error result handling · `src/XChange/XChange.Core/Common/Result.cs`
- [Errors](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Core/Exceptions): exceptions · Core exception hierarchy for user and MFA flows · `src/XChange/XChange.Core/Exceptions`

### XChange.Infrastructure

Concrete SQL and service implementations

- [SQL Factory](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Infrastructure/Data/SqlConnectionFactory.cs): db access · Creates SQL connections for persistence operations · `src/XChange/XChange.Infrastructure/Data/SqlConnectionFactory.cs`
- [User Repos](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Infrastructure/Repositories): repositories · SQL-backed user and 2FA repositories · `src/XChange/XChange.Infrastructure/Repositories`
- [Security Services](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Infrastructure/Services): crypto/mfa · Password hashing and TOTP/MFA implementation services · `src/XChange/XChange.Infrastructure/Services`

### XChange.Web

MVC presentation layer and user flows

- [Program](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Program.cs): host startup · ASP.NET Core entry point and dependency wiring · `src/XChange/XChange.Web/Program.cs`
- [Auth Controller](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Controllers/AuthController.cs): mvc controller · Registration, login, password recovery, email verification, and MFA flows · `src/XChange/XChange.Web/Controllers/AuthController.cs`
- [User Controller](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Controllers/UserController.cs): mvc controller · Private account area for dashboard, wallet, transactions, and settings · `src/XChange/XChange.Web/Controllers/UserController.cs`
- [Home Controller](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Controllers/HomeController.cs): mvc controller · Public landing and informational pages · `src/XChange/XChange.Web/Controllers/HomeController.cs`
- [MVC Filters](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Filters): request guards · Redirect rules for authenticated users and MFA login flow · `src/XChange/XChange.Web/Filters`
- [Auth ViewModels](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/ViewModels): view models · Login, register, reset, email verify, and MFA form models · `src/XChange/XChange.Web/ViewModels`
- [Auth Views](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Views/Auth): razor views · UI pages for the identity lifecycle · `src/XChange/XChange.Web/Views/Auth`
- [Private Views](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Views/User): razor views · Authenticated dashboard, wallet, transactions, and settings pages · `src/XChange/XChange.Web/Views/User`
- [Public Views](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Views/Home): razor views · Landing and informational site pages · `src/XChange/XChange.Web/Views/Home`
- [Layouts](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/Views/Shared): razor layout · Shared public, auth, private, and base layouts · `src/XChange/XChange.Web/Views/Shared`
- [Static Assets](https://github.com/emanuelmath/xchange/blob/main/src/XChange/XChange.Web/wwwroot): wwwroot assets · Bootstrap, Font Awesome, custom CSS/JS, and localized client-side data · `src/XChange/XChange.Web/wwwroot`

## Connections

- Program → Contracts: registers · Wires abstractions into dependency injection
- Program → SQL Factory: configures · Sets up database connectivity and hosting services
- Program → User Repos: registers · Adds repository implementations to the container
- Program → Security Services: registers · Adds hashing and MFA services to the container
- Auth Controller → Contracts: uses · Depends on repository and service interfaces
- Auth Controller → Auth ViewModels: binds · Consumes dedicated authentication form models
- Auth Controller → MVC Filters: governed by · Protected by redirect and login-state filters
- User Controller → Contracts: uses · Reads and writes user/account state through abstractions
- User Controller → MVC Filters: governed by · Protected by request gating rules
- Home Controller → Layouts: renders · Served through shared public layouts
- Auth Controller → Auth Views: renders · Returns identity workflow views
- User Controller → Private Views: renders · Returns authenticated account views
- Home Controller → Public Views: renders · Returns public-facing pages
- Auth Views → Layouts: uses · Shares auth-specific and base layouts
- Private Views → Layouts: uses · Shares private authenticated layouts
- Public Views → Layouts: uses · Shares public and base layouts
- Auth Views → Static Assets: loads · Uses CSS, JS, and localized assets
- Private Views → Static Assets: loads · Uses shared static UI assets
- Contracts → Domain: defines · Interfaces operate on core entities and enums
- Contracts → Result: returns · Service and repository operations use result objects
- Errors → Result: maps to · Exceptions support structured failure handling
- User Repos → Contracts: implements · Concrete repositories satisfy core interfaces
- Security Services → Contracts: implements · Concrete security services satisfy core interfaces
- SQL Factory → Contracts: implements · SQL connection factory fulfills database contract
- User Repos → SQL Factory: opens · Repositories acquire connections through the factory
- Security Services → Domain: works with · Hashing and MFA services operate on user-related domain data
- Auth Controller → Errors: handles · Controller flow can surface domain exceptions
- User Controller → Domain: uses · Displays and updates account state concepts
