# ryan-h/dependencyinjection.servicefactory architecture

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

- Interactive diagram: https://gitdiagram.com/ryan-h/dependencyinjection.servicefactory
- Repository: https://github.com/ryan-h/dependencyinjection.servicefactory

## Overview

##### Project type and purpose
- This is a .NET Core solution containing:
- A class‐library “App.Extensions.DependencyInjection.ServiceFactory” providing an IServiceFactory<T> abstraction on top of Microsoft.Extensions.DependencyInjection.
- An “Example” ASP.NET-style console/web host demonstrating how to wire up and use the factory extension to select at runtime between different repository implementations (Cosmos, Couchbase) via configuration.

##### File structure
- Top‐level projects:
- App.Extensions.DependencyInjection.ServiceFactory (the DI‐factory library)
- Example (the demo application)
- Library project files:
- IServiceFactory.cs – factory interface
- ServiceCollectionExtensions.cs – AddFactory<TService, TFactory> extension
- Example project files:
- Models/RepositoryOptions.cs – options pattern model bound from appsettings.json
- Services/Interfaces – IExampleService, IExampleRepository
- Services/Factories – ExampleRepositoryFactory : IServiceFactory<IExampleRepository>
- Services/Repositories – CosmosExampleRepository, CouchbaseExampleRepository
- ExampleService – business‐logic service depending on IExampleRepository
- Startup.cs & Program.cs – host setup, configuration binding, DI registrations

##### Main components & their relationships
a. Configuration subsystem
- appsettings.json → bound to RepositoryOptions via IOptions<RepositoryOptions>
b. DI Container (Microsoft.Extensions.DependencyInjection)
- Registers ServiceFactory library’s AddFactory<T,TF>()
- Registers ExampleRepositoryFactory as factory for IExampleRepository
- Registers ExampleService as consumer of IExampleRepository
c. Service Factory layer
- IServiceFactory<IExampleRepository>.Create(provider) reads RepositoryOptions to decide which repository implementation to instantiate
d. Domain Service layer
- ExampleService calls IExampleRepository methods
e. Repository implementations
- CosmosExampleRepository → interacts with Cosmos DB
- CouchbaseExampleRepository → interacts with Couchbase cluster
f. External resources
- Cosmos DB endpoint
- Couchbase cluster

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "ServiceFactory Library"
        IF["IServiceFactory<T>"]:::library
        SC["ServiceCollectionExtensions"]:::library
    end

    subgraph "Example Application"
        APPSET["appsettings.json"]:::app
        REP_OPT["RepositoryOptions"]:::app
        STARTUP["Startup.cs"]:::app
        PROGRAM["Program.cs"]:::app
        DI["Microsoft DI Container"]:::app
        IEXSVC_IF["IExampleService"]:::app
        EXSVC["ExampleService"]:::app
        IEXREP_IF["IExampleRepository"]:::app
        REP_FACTORY["ExampleRepositoryFactory"]:::app
        COS_REP["CosmosExampleRepository"]:::app
        CB_REP["CouchbaseExampleRepository"]:::app
    end

    COS_DB["Cosmos DB Endpoint"]:::external
    CB_CLUSTER["Couchbase Cluster"]:::external

    APPSET -->|bind via IOptions| REP_OPT
    REP_OPT -->|provides options to| DI
    PROGRAM -->|configures host| STARTUP
    STARTUP -->|calls AddFactory| SC
    SC -->|extends| DI
    DI -->|resolves IServiceFactory<IExampleRepository>| IF
    DI -->|resolves IExampleRepository via factory| REP_FACTORY
    REP_FACTORY -->|implements IServiceFactory| IF
    REP_FACTORY -->|instantiates CosmosExampleRepository| COS_REP
    REP_FACTORY -->|instantiates CouchbaseExampleRepository| CB_REP
    DI -->|resolves IExampleService| EXSVC
    EXSVC -->|implements IExampleService| IEXSVC_IF
    EXSVC -->|calls repository methods on| IEXREP_IF
    COS_REP -->|interacts with| COS_DB
    CB_REP -->|interacts with| CB_CLUSTER

    click IF "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/App.Extensions.DependencyInjection.ServiceFactory/IServiceFactory.cs"
    click SC "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/App.Extensions.DependencyInjection.ServiceFactory/ServiceCollectionExtensions.cs"
    click IF "https://github.com/ryan-h/dependencyinjection.servicefactory/tree/master/App.Extensions.DependencyInjection.ServiceFactory/"
    click APPSET "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/Example/appsettings.json"
    click REP_OPT "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/Example/Models/RepositoryOptions.cs"
    click STARTUP "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/Example/Startup.cs"
    click PROGRAM "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/Example/Program.cs"
    click IEXSVC_IF "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/Example/Services/Interfaces/IExampleService.cs"
    click EXSVC "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/Example/Services/ExampleService.cs"
    click IEXREP_IF "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/Example/Services/Interfaces/IExampleRepository.cs"
    click REP_FACTORY "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/Example/Services/Factories/ExampleRepositoryFactory.cs"
    click COS_REP "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/Example/Services/Repositories/CosmosExampleRepository.cs"
    click CB_REP "https://github.com/ryan-h/dependencyinjection.servicefactory/blob/master/Example/Services/Repositories/CouchbaseExampleRepository.cs"

    classDef library fill:#ADD8E6,stroke:#333,stroke-width:1px;
    classDef app fill:#90EE90,stroke:#333,stroke-width:1px;
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px;
```
