# canhhungit/miniunitofwork architecture

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

- Interactive diagram: https://gitdiagram.com/canhhungit/miniunitofwork
- Repository: https://github.com/canhhungit/miniunitofwork

## Overview

1. MiniUow – The core library that provides repository and unit-of-work implementations, along with dependency injection helpers (see files such as BaseRepository.cs, IRepository.cs, IUnitOfWork.cs, UnitOfWork.cs, and the DependencyInjection folder). This is the “engine” that developers integrate into their applications.
2. CommonUtils – An auxiliary library providing several common utilities (extensions methods, caching utilities, helper functions, Excel processing with NPOI, etc.). Although it’s not directly part of the repository/unit-of-work abstraction, it supports common functionality that might be used in a project consuming MiniUow.

A. Identify the Main Components:
- Client/Consumer Layer: Represents any application (web API, service, or console app) that integrates MiniUow via dependency injection, as illustrated in the README’s Startup.cs example.
- Dependency Injection Setup: This includes the configuration in Startup.cs where the DbContext and the Unit of Work are registered (e.g., AddDbContext<SampleContext>().AddUnitOfWork<SampleContext>()). This is critical since the library is integrated using DI.
- Repository Component: Core interfaces and classes (IRepository, BaseRepository, Repository) that abstract data access. These act as the mediator between business logic and the lower-level data access.
- Unit-of-Work Component: Interfaces and classes (IUnitOfWork, UnitOfWork) that coordinate multiple repository operations within a single transaction.
- Data Access Layer: Though not directly implemented in this library, the integration with Entity Framework Core (via various DbContext configurations for SQL Server, PostgreSQL, or MySQL as shown in the readme) constitutes the data persistence layer.
- Common Utilities: The CommonUtils library providing additional helper methods (caching, extensions, Excel processing, etc.) that support cross-cutting concerns in data manipulation and business logic.

B. Determine the Relationships and Interactions:
- How the Repository components interact with the underlying ORM (Entity Framework Core), which in turn interacts with the database.
- Optionally, show how CommonUtils might be used within these layers for support functions (for example, caching utilities might be used within Repository implementations for performance improvements).

C. Highlight Architectural Patterns and Design Principles:
- Repository Pattern: Emphasize how the repository interfaces encapsulate data access logic.
- Unit-of-Work Pattern: Show the role of IUnitOfWork and its implementation for managing transactions.
- Dependency Injection: Highlight the separation of concerns enabled by injecting the Unit of Work into controllers or services.
- Abstraction and Extensibility: Note that developers can plug in different data providers (SQL Server, PostgreSQL, MySQL) with minimal code changes.

D. Include Relevant Technologies and Frameworks:
- .NET Core / .NET Standard: The base framework supporting the library.
- Entity Framework Core: The ORM used for data access.
- NuGet: Distribution and package management.
- Optionally, note any testing frameworks or external dependencies if they become part of the extended architecture.

- The separation between the client’s presentation logic (e.g., controllers) and the repository/unit-of-work layer.
- The data flow from client calls to repository methods, unit-of-work coordination, and database operations.
- Integration points where developers can use CommonUtils for additional functionality.

- Clearly labeled components: “Client / Controller”, “Dependency Injection Configuration”, “IRepository & Repository”, “IUnitOfWork & UnitOfWork”, “DbContext / EF Core”, “Database”, and “Common Utilities.”
- How external application components invoke the repository and unit-of-work.
- The flow from Unit-of-Work to individual repositories.
- The communication pathway from repositories to the DbContext, and thus, to the physical database.
- Architectural layers (Presentation, Data Access, Infrastructure).
- Static libraries (MiniUow versus CommonUtils).
- Annotations that describe how DI is used in the startup configuration and how the library allows for extensibility (e.g., different database providers with minimal configuration changes).

- Be as granular as possible by separating the system into components that align with the core functionalities (repository, unit-of-work, DI).
- Inherent abstractions (interface-implementation pairs) that allow extensibility.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    Client["Client / Controller"]:::presentation

    %% Dependency Injection Configuration
    DI["Dependency Injection\nConfiguration (Startup.cs)"]:::di

    %% MiniUow - Core Library Components
    subgraph "MiniUow (Core Library)"
        IRepository["IRepository Interface"]:::core
        BaseRepository["BaseRepository Implementation"]:::core
        Repository["Repository Implementation"]:::core
        IUnitOfWork["IUnitOfWork Interface"]:::core
        UnitOfWork["UnitOfWork Implementation"]:::core
        DI_Ext["DI Extensions"]:::core
    end

    %% Data Access
    DbContext["DbContext / EF Core"]:::data
    Database["Database"]:::database

    %% Common Utilities
    CommonUtils["Common Utilities"]:::utils

    %% Connections
    Client -->|"calls"| DI
    DI -->|"registers"| DI_Ext
    DI_Ext -->|"wires"| IRepository
    DI_Ext -->|"wires"| IUnitOfWork

    IRepository -->|"uses"| BaseRepository
    BaseRepository -->|"inherits"| Repository
    Repository -->|"operatesOn"| DbContext
    IUnitOfWork -->|"coordinates"| UnitOfWork
    UnitOfWork -->|"manages"| IRepository

    DbContext -->|"communicatesWith"| Database

    Repository -->|"mayUse"| CommonUtils

    %% Click Events for MiniUow Components
    click IRepository "https://github.com/canhhungit/miniunitofwork/blob/master/MiniUow/IRepository.cs"
    click BaseRepository "https://github.com/canhhungit/miniunitofwork/blob/master/MiniUow/BaseRepository.cs"
    click Repository "https://github.com/canhhungit/miniunitofwork/blob/master/MiniUow/Repository.cs"
    click IUnitOfWork "https://github.com/canhhungit/miniunitofwork/blob/master/MiniUow/IUnitOfWork.cs"
    click UnitOfWork "https://github.com/canhhungit/miniunitofwork/blob/master/MiniUow/UnitOfWork.cs"
    click DI_Ext "https://github.com/canhhungit/miniunitofwork/blob/master/MiniUow/DependencyInjection/UnitOfWorkServiceCollectionExtentions.cs"

    %% Click Event for Common Utilities
    click CommonUtils "https://github.com/canhhungit/miniunitofwork/tree/master/CommonUtils/CommonUtil"

    %% Class Definitions for Colors and Styles
    classDef presentation fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef di fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef core fill:#D5F5E3,stroke:#28B463,stroke-width:2px;
    classDef data fill:#FADBD8,stroke:#CB4335,stroke-width:2px;
    classDef database fill:#F5EEF8,stroke:#8E44AD,stroke-width:2px;
    classDef utils fill:#FCF3CF,stroke:#F1C40F,stroke-width:2px;
```
