# nguyenhbtrung/electro-shop-backend architecture

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

- Interactive diagram: https://gitdiagram.com/nguyenhbtrung/electro-shop-backend
- Repository: https://github.com/nguyenhbtrung/electro-shop-backend

## Overview

##### Main Components
- Entry Point & Configuration: Start with Program.cs and appsettings.json which bootstrap the application and house critical configurations like database connections, JWT authentication settings, email configuration, and VNPay parameters.
- API Layer (Controllers): This layer contains multiple controllers (e.g., BannerController, BrandController, CartController, etc.) that receive HTTP requests. Each controller corresponds to a distinct domain (product, order, cart, user, etc.).
- Service Layer: Represented by services (e.g., BannerService, CartService, OrderService) and their corresponding interfaces (located in Services/Interfaces). This layer contains the business logic and orchestrates interactions between the controllers, the data layer, and external integrations.
- Data Access Layer: Illustrated by the ApplicationDBContext in the Data folder, along with the Migrations directory. This layer is responsible for database operations using Entity Framework Core, mapping to the Models/Entities.
- Domain Models: These are split into several areas:
- DTOs (Data Transfer Objects) for communication between layers (in Models/DTOs).
- Entities representing the persistent data models (in Models/Entities).
- Mappers to convert between DTOs and Entities.
- Exceptions & Helpers: Custom exception classes help handle error scenarios while Helpers and Extensions provide utility functionalities (like query handling or VnPay integration).
- Real-Time Communication: The Hubs directory, with ChatHub as an example, indicates integration with SignalR for real-time communication.
- External Integrations: Configured within appsettings.json and supported by services such as VnPayService, EmailService, etc. They let the system process payments and send out emails.

##### Relationships & Data Flow
- Requests arrive at the Controllers which then invoke the corresponding Service methods.
- Services handle business logic, utilize Helpers and Extensions where needed, and interact with the Data Access Layer (ApplicationDBContext) for CRUD operations.
- The Data Access Layer communicates with a relational database (configured via DefaultSQLConnection) and is evolved via Migrations.
- External service integrations (e.g., VNPay, Email) occur in the Service layer where services (like VnPayService and EmailService) integrate with their respective providers.
- Real-time interactions (ChatHub) may be invoked from controllers or services to handle notifications/messages.

##### Architectural Patterns
- Layered Architecture: Clearly separated presentation (API), business logic (Services), and data persistence layers.
- Dependency Injection: Evident from the use of interfaces in Services/Interfaces.
- MVC/Mediated Pattern: Controllers act as mediators that route requests to Services.
- Real-Time Communication Pattern: Using SignalR Hubs.

##### Technology Stack
- ASP.NET Core & C# for backend development.
- Entity Framework Core for data handling.
- SignalR for real-time updates.
- Integration with VNPay and SMTP-based email services.
- JWT for authentication/authorization.
- Visual Studio for development and debugging.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Point & Configurations
    subgraph "Entry Point & Configurations"
        E1["Program.cs"]:::entry
        E2["Email Configuration"]:::entry
    end

    %% API Layer (Controllers)
    subgraph "API Layer (Controllers)"
        C1["Controllers (BannerController, CartController, etc.)"]:::controller
    end

    %% Business Logic Layer (Services)
    subgraph "Business Logic Layer (Services)"
        S1["Services (BannerService, CartService, OrderService, etc.)"]:::service
        S2["Service Interfaces"]:::service
    end

    %% Domain Models
    subgraph "Domain Models"
        D1["Entities"]:::domain
        D2["DTOs"]:::domain
        D3["Mappers"]:::domain
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        DA1["ApplicationDBContext"]:::data
        DA2["Migrations"]:::data
    end

    %% Utilities & External Integrations
    subgraph "Utilities & External Integrations"
        U1["Exceptions"]:::util
        U2["Helpers & Extensions"]:::util
        U3["ChatHub (Real-Time)"]:::hub
        U4["Email Service"]:::external
        U5["VNPay Service"]:::external
    end

    %% Connections: Entry Point to API
    E1 -->|"bootstrap"| C1
    %% Entry Point Configuration used by external integrations
    E2 -->|"provides config"| U4

    %% API to Business Logic
    C1 -->|"handles requests"| S1

    %% Business Logic to Domain Models
    S1 -->|"uses"| D1
    S1 -->|"uses"| D2
    S1 -->|"uses"| D3

    %% Business Logic to Data Access
    S1 -->|"CRUD operations"| DA1
    DA1 -->|"schema evolution"| DA2

    %% Business Logic to External Integrations & Utilities
    S1 -->|"integrates"| U4
    S1 -->|"integrates"| U5
    S1 -->|"logs/handles errors"| U1
    S1 -->|"utilizes"| U2

    %% API Layer interacts with real-time hub
    C1 -->|"triggers notifications"| U3

    %% Click Events
    click E1 "https://github.com/nguyenhbtrung/electro-shop-backend/blob/main/Program.cs"
    click E2 "https://github.com/nguyenhbtrung/electro-shop-backend/blob/main/Configurations/EmailConfiguration.cs"
    click C1 "https://github.com/nguyenhbtrung/electro-shop-backend/tree/main/Controllers"
    click S1 "https://github.com/nguyenhbtrung/electro-shop-backend/tree/main/Services"
    click S2 "https://github.com/nguyenhbtrung/electro-shop-backend/tree/main/Services/Interfaces"
    click DA1 "https://github.com/nguyenhbtrung/electro-shop-backend/blob/main/Data/ApplicationDBContext.cs"
    click DA2 "https://github.com/nguyenhbtrung/electro-shop-backend/tree/main/Migrations"
    click D1 "https://github.com/nguyenhbtrung/electro-shop-backend/tree/main/Models/Entities"
    click D2 "https://github.com/nguyenhbtrung/electro-shop-backend/tree/main/Models/DTOs"
    click D3 "https://github.com/nguyenhbtrung/electro-shop-backend/tree/main/Models/Mappers"
    click U1 "https://github.com/nguyenhbtrung/electro-shop-backend/tree/main/Exceptions"
    click U2 "https://github.com/nguyenhbtrung/electro-shop-backend/tree/main/Helpers/Extensions"
    click U3 "https://github.com/nguyenhbtrung/electro-shop-backend/blob/main/Hubs/ChatHub.cs"
    click U4 "https://github.com/nguyenhbtrung/electro-shop-backend/blob/main/Services/EmailService.cs"
    click U5 "https://github.com/nguyenhbtrung/electro-shop-backend/blob/main/Services/VnPayService.cs"

    %% Styles
    classDef entry fill:#ffcccc,stroke:#d62728,stroke-width:2px;
    classDef controller fill:#ccffcc,stroke:#2ca02c,stroke-width:2px;
    classDef service fill:#ffffcc,stroke:#ff7f0e,stroke-width:2px;
    classDef domain fill:#e6ccff,stroke:#9467bd,stroke-width:2px;
    classDef data fill:#cce5ff,stroke:#1f77b4,stroke-width:2px;
    classDef util fill:#ffebcc,stroke:#8c564b,stroke-width:2px;
    classDef hub fill:#f0e68c,stroke:#b5bd68,stroke-width:2px;
    classDef external fill:#d9ead3,stroke:#17becf,stroke-width:2px;
```
