# remononnorokom/di_service_lifetime architecture

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

- Interactive diagram: https://gitdiagram.com/remononnorokom/di_service_lifetime
- Repository: https://github.com/remononnorokom/di_service_lifetime

## Overview

##### Main Components
a. Composition Root
- This is the entry point (e.g. `Program.cs` or `Startup.cs`) where you build the `IServiceCollection` and call `BuildServiceProvider()`.
b. DI Container (`ServiceProvider`)
- The container that holds registrations and resolves instances.
c. Services with Different Lifetimes
- TransientService (registered with `AddTransient<T>`)
- ScopedService (registered with `AddScoped<T>`)
- SingletonService (registered with `AddSingleton<T>`)
d. Consumer(s) / Client(s)
- Classes (e.g. controllers, console commands) that request those services.
e. (If Web API) HTTP Request Pipeline
- Scope creation per incoming request.
f. (If Console) Manual Scope / Using `IServiceScopeFactory`

##### Relationships and Data Flows
- The Composition Root ➔ registers all services with distinct lifetimes.
- At runtime the ServiceProvider ➔ resolves services
- Transient: new instance every time it’s requested
- Scoped: one instance per scope (e.g. per HTTP request or per manually created scope)
- Singleton: one instance for the entire container lifetime
- Consumer(s) depend on these services via constructor injection.

##### Important Architectural Patterns
- Inversion of Control (IoC) / Dependency Injection
- Lifetime management as a cross-cutting concern
- If Web API, the scoped lifetime aligns with the request pipeline pattern

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Composition Root and Container
    CompositionRoot["Composition Root\n(Program.cs)"]:::compRoot
    ServiceProvider["ServiceProvider\n(DI Container)"]:::container

    CompositionRoot -->|"Register Transient\nAddTransient<T>"| ServiceProvider
    CompositionRoot -->|"Register Scoped\nAddScoped<T>"| ServiceProvider
    CompositionRoot -->|"Register Singleton\nAddSingleton<T>"| ServiceProvider

    %% Services Definitions
    TransientService["TransientService"]:::transient
    ScopedService["ScopedService"]:::scoped
    SingletonService["SingletonService"]:::singleton

    ServiceProvider -->|"new each resolve"| TransientService
    ServiceProvider -->|"once per scope"| ScopedService
    ServiceProvider -->|"once globally"| SingletonService

    %% Runtime Scopes
    subgraph "Scope #1" 
        direction TB
        S1_Transient["TransientService\n(instance #1)"]:::transient
        S1_Scoped["ScopedService\n(instance #1)"]:::scoped
        S1_Singleton["SingletonService\n(instance #1)"]:::singleton
        ServiceProvider -->|"CreateScope()"| S1_Scoped
        ServiceProvider -->|"Resolve<T> within scope"| S1_Transient
        ServiceProvider -->|"Resolve<T> within scope"| S1_Singleton
    end

    subgraph "Scope #2"
        direction TB
        S2_Transient["TransientService\n(instance #2)"]:::transient
        S2_Scoped["ScopedService\n(instance #2)"]:::scoped
        S2_Singleton["SingletonService\n(instance #1)"]:::singleton
        ServiceProvider -->|"CreateScope()"| S2_Scoped
        ServiceProvider -->|"Resolve<T> within scope"| S2_Transient
        ServiceProvider -->|"Resolve<T> within scope"| S2_Singleton
    end

    %% Consumer
    Consumer["Consumer\n(ConsoleRunner)"]:::consumer
    S1_Scoped -->|"Injected in ctor"| Consumer
    S1_Transient -->|"Injected in ctor"| Consumer
    S1_Singleton -->|"Injected in ctor"| Consumer

    %% Legend
    subgraph "Legend"
        direction TB
        CR[Composition Root]:::compRoot
        CT[DI Container]:::container
        TS[Transient Service]:::transient
        SS[Scoped Service]:::scoped
        SLS[Singleton Service]:::singleton
        CU[Consumer]:::consumer
    end

    classDef compRoot fill:#add8e6,stroke:#333,stroke-width:1px;
    classDef container fill:#d3d3d3,stroke:#333,stroke-width:1px;
    classDef transient fill:#98fb98,stroke:#333,stroke-width:1px;
    classDef scoped fill:#ffa500,stroke:#333,stroke-width:1px;
    classDef singleton fill:#f08080,stroke:#333,stroke-width:1px;
    classDef consumer fill:#dda0dd,stroke:#333,stroke-width:1px;
    classDef boundary fill:#eeeeee,stroke:#666,stroke-dasharray: 5 5;
    classDef legend fill:#fffacd,stroke:#333,stroke-width:1px;
```
