# lidiadev/ecommerce-api architecture

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

- Interactive diagram: https://gitdiagram.com/lidiadev/ecommerce-api
- Repository: https://github.com/lidiadev/ecommerce-api

## Overview

##### Main layers and components
a. Domain Layer: This is where the core business logic resides. In the file tree, this corresponds to the "Domain" directory that contains entities (e.g., Customer, Order, OrderLine, and domain-specific value objects and interfaces such as IRepository, IUnitOfWork, etc.).
b. Application Layer: Found in the "Application" directory, this layer contains business logic mediator elements such as commands and queries (e.g., CreateCustomerCommand, GetCustomerQuery) that use the domain abstractions and handle business use cases.
c. Infrastructure Layer: Under the "Infrastructure" directory, this layer implements the persistence aspects through repository implementations and Entity Framework Core configurations. It also contains migration files and DB context configuration.
d. Web API Layer: The "ECommerceAPI" directory represents the entry point for external interactions. This includes the controllers (CustomersController, OrdersController), API setup (Startup.cs, Program.cs), and filters for error handling.
e. Tests: Although not part of the production architecture, the "tests" directory includes both unit tests (Domain.UnitTests) and integration tests (ECommerce.IntegrationTests) that verify behavior across the layers.

##### Relationships and interactions
- The Web API layer depends on both the Application and Infrastructure layers. It exposes HTTP endpoints through controllers that invoke application services and use dependency injection to resolve services.
- The Application layer is independent of lower-level concerns—it defines commands and queries that encapsulate use cases. It interacts with the domain layer to enforce business rules and invariants.
- The Domain layer is pure business logic and is not dependent on any external or infrastructure-specific code.
- The Infrastructure layer implements interfaces defined in the Application or Domain layers. For example, it provides concrete implementations of repository interfaces (CustomerRepository, OrderRepository) and configures the database using EF Core.

##### Important architectural patterns and design principles
- Clean Architecture: The separation into Domain, Application, Infrastructure, and API layers is a best practice that isolates business rules from UI and persistence concerns.
- Domain-Driven Design (DDD): The Domain layer includes entities, value objects, and repository interfaces, and the Application layer holds use-case logic.
- Dependency Injection: Clearly visible in the Startup.cs and infrastructure dependency injection projects where services and their concrete implementations are wired together.
- CQRS & MediatR: Commands (like CreateOrderCommand, CreateCustomerCommand) and queries (like GetCustomerQuery, GetCustomersQuery) separate write and read operations.
- Testing: The existence of unit tests and integration tests underlines the emphasis on testability.

##### Technologies and frameworks that should be captured
- .NET Core 3.1 (for both the API and backend services)
- ASP.NET Core (for the REST API layer)
- Entity Framework Core 3.1 (ORM for the Infrastructure layer)
- MediatR (for dispatching commands/queries)
- NUnit and FluentAssertions (for testing)

##### Final assembly and note on detail
- Every component is labeled: the Domain (with entities and interfaces), the Application (commands, query handlers), the Infrastructure (repositories, EF Core configurations), and the Web API (controllers, middleware).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Web API Layer
    subgraph "Web API Layer"
        A["CustomersController"]:::api
        B["OrdersController"]:::api
        C["Startup.cs"]:::api
        D["Program.cs"]:::api
    end

    %% Application Layer
    subgraph "Application Layer"
        E["CreateCustomerCommand"]:::app
        F["CreateOrderCommand"]:::app
        G["GetCustomerQuery"]:::app
    end

    %% Domain Layer
    subgraph "Domain Layer"
        H["Customer Entity"]:::domain
        I["Order Entity"]:::domain
        J["OrderLine Value Object"]:::domain
        K["Repository & UnitOfWork Interfaces"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        L["EcommerceDbContext"]:::infra
        M["CustomerRepository"]:::infra
        N["OrderRepository"]:::infra
        O["EF Migrations"]:::infra
    end

    %% Testing Layer
    subgraph "Testing"
        P["Domain.UnitTests"]:::tests
        Q["ECommerce.IntegrationTests"]:::tests
    end

    %% Relationships between Web API and Application
    A -->|"HTTP_Request"| E
    A -->|"HTTP_Request"| G
    B -->|"HTTP_Request"| F

    %% Relationships between Application and Domain
    E -->|"Executes"| H
    F -->|"Executes"| I
    G -->|"Retrieves"| H

    %% Relationships between Domain and Infrastructure
    H -->|"Persists_via"| M
    I -->|"Persists_via"| N

    %% Infrastructure dependencies
    M -->|"Uses"| L
    N -->|"Uses"| L

    %% Testing links (dashed for test coverage)
    P -.-> H
    Q -.-> A

    %% Click Events for Web API Layer
    click A "https://github.com/lidiadev/ecommerce-api/blob/master/ECommerceAPI/Controllers/CustomersController.cs"
    click B "https://github.com/lidiadev/ecommerce-api/blob/master/ECommerceAPI/Controllers/OrdersController.cs"
    click C "https://github.com/lidiadev/ecommerce-api/blob/master/ECommerceAPI/Startup.cs"
    click D "https://github.com/lidiadev/ecommerce-api/blob/master/ECommerceAPI/Program.cs"

    %% Click Events for Application Layer
    click E "https://github.com/lidiadev/ecommerce-api/blob/master/Application/Customers/CreateCustomerCommand.cs"
    click F "https://github.com/lidiadev/ecommerce-api/blob/master/Application/Orders/CreateOrderCommand.cs"
    click G "https://github.com/lidiadev/ecommerce-api/blob/master/Application/Customers/GetCustomerQuery.cs"

    %% Click Events for Domain Layer
    click H "https://github.com/lidiadev/ecommerce-api/tree/master/Domain/Customers"
    click I "https://github.com/lidiadev/ecommerce-api/tree/master/Domain/Orders"
    click K "https://github.com/lidiadev/ecommerce-api/tree/master/Domain"

    %% Click Events for Infrastructure Layer
    click L "https://github.com/lidiadev/ecommerce-api/tree/master/Infrastructure/Persistence/EcommerceDbContext"
    click M "https://github.com/lidiadev/ecommerce-api/blob/master/Infrastructure/Repositories/CustomerRepository.cs"
    click N "https://github.com/lidiadev/ecommerce-api/blob/master/Infrastructure/Repositories/OrderRepository.cs"
    click O "https://github.com/lidiadev/ecommerce-api/tree/master/Infrastructure/Migrations"

    %% Click Events for Testing Layer
    click P "https://github.com/lidiadev/ecommerce-api/blob/master/tests/Domain.UnitTests"
    click Q "https://github.com/lidiadev/ecommerce-api/blob/master/tests/ECommerce.IntegrationTests"

    %% Styles
    classDef api fill:#ffffcc,stroke:#333,stroke-width:2px;
    classDef app fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef domain fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef infra fill:#ccccff,stroke:#333,stroke-width:2px;
    classDef tests fill:#e0e0e0,stroke:#333,stroke-width:2px;
```
