# q7164518/sharpredis architecture

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

- Interactive diagram: https://gitdiagram.com/q7164518/sharpredis
- Repository: https://github.com/q7164518/sharpredis

## Overview

##### Project type and purpose
- SharpRedis is a high-performance, fully asynchronous C# Redis driver/library, supporting .NET 3.0 through .NET 8. It is not a web app or microservice—it’s a client‐side SDK that applications reference to communicate with a Redis server.

##### File structure
- Top-level directories:
- src/SharpRedis: the core library (Commands, Provider, Network, Models, Extensions, Values)
- src/SharpRedis.* (Autofac, DependencyInjection, DependencyInjectionKeyedService, Unity): DI/IoC integration projects
- examples/: sample console applications demonstrating usage on multiple .NET versions
- test/: automated tests against the provider and data types
- Core patterns:
- Asynchronous, event-driven API
- Command modules separated by Redis data type (String, Hash, List, etc.)
- Provider layer (Calls → Redis operations)
- Network layer (Connection, ConnectionPool, Master-Slave pool)
- Extensions for older .NET versions and extra behaviors (e.g., client-side caching)
- Dependency Injection registration extensions

##### README for additional insights
- Lists key features: asynchronous driver, local caching, pipeline/transaction support, DI/IoC integration, broad .NET coverage
- Shows usage snippets for standalone, pipeline, transaction, switching DB, local cache, DI libraries

##### Main components
a. Frontend/Consumer
- Example Applications (examples/*.csproj)
- Consumer .NET application code referencing SharpRedis
b. DI/IoC Integration Layer
- SharpRedis.DependencyInjection
- SharpRedis.DependencyInjectionKeyedService
- SharpRedis.Autofac
- SharpRedis.Unity
- These projects expose RegistrationExtensions to inject SharpRedis into IServiceCollection, Autofac ContainerBuilder, or UnityContainer.
c. SharpRedis Core API Layer
- Redis (static entry point, e.g., Redis.UseStandalone, .UseCluster, .UseSentinel)
- Redis.String, Redis.Hash, Redis.List, Redis.Set, Redis.SortedSet, Redis.Stream, Redis.PubSub, Redis.Transaction, Redis.Pipeline, etc.
d. Command Modules
- src/SharpRedis/Commands/*.cs: Command-specific classes implementing each data type’s commands
e. Provider Layer
- src/SharpRedis/Provider:
- Calls (DefaultCall, PipeliningCall, TransactionCall, MasterSlaveCall, etc.)
- RedisPipelining, RedisTransaction, RedisSwitchDatabase
- Standard BaseCall, BaseRedis, and feature-specific Redis classes
- Types: RedisString, RedisHash, RedisList, RedisSet, … and their Async/Span variants
f. Network Layer
- src/SharpRedis/Network:
- DefaultConnection, SubConnection
- ConnectionPool implementations: DefaultPool, MasterSlavePool
- Interfaces: IConnection, IConnectionPool
g. Models & Values
- CommandPacket, ConnectionOptions, OnReceiveModel, standard model interfaces
- Value wrappers: BooleanValue, NumberValue, StreamValue, XInfo*Value, etc.
h. Extensions
- Client-side caching, convert extensions, data packet extensions, and legacy .NET support under Extensions folder
i. External Systems
- Redis Server(s): standalone, cluster, sentinel

##### Relationships and interactions (arrowed flows)
1. Consumer App → (via DI or direct call) SharpRedis.Core.Redis
2. SharpRedis.Core.Redis → Command Module (e.g., Redis.String.Set)
3. Command Module → Provider.Call (DefaultCall or specialized call)
4. Provider.Call → Network Layer (IConnectionPool.Request → IConnection.WriteAsync)
5. Network Layer → TCP connection → Redis Server
6. Redis Server → TCP response → Network Layer → Provider (parses into Value types) → returns to Consumer
7. Pipeline/Transaction: commands buffered in Provider (PipeliningCall, TransactionCall) then ExecutePipelining or Exec triggers a batch → Network
8. Local Cache Extensions intercept calls and responses if enabled (in Extensions → ClientSideCachingExtensions)

##### Architectural patterns & design principles
- Layered architecture: API → Commands → Provider → Network → External
- Dependency Inversion: interfaces for IConnection, IConnectionPool, BaseCall, BaseRedis
- Strategy/Factory for different call types (default, pipeline, transaction, master-slave)
- Separation of concerns: each Redis data type in its own module
- Asynchronous I/O everywhere using async/await and CancellationToken
- DI/IoC registration extensions for pluggability in .NET DI, Autofac, Unity
- Extension points for local caching, custom models, and older .NET support

##### Final advice
- Async patterns: annotate where async operations occur and where CancellationToken is passed.
- Pluggable parts: DI modules, custom local cache, RESP protocol version.
- Use layering and proper grouping so a reader immediately grasps the flow from application to Redis server.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Consumer Layer
    subgraph "Consumer Layer"
        Consumer["Consumer Applications"]:::consumer
    end

    %% DI Integration
    subgraph "DI/IoC Integration" 
        DI_Default[".NET DI RegistrationExtensions"]:::di
        DI_Keyed["KeyedService RegistrationExtensions"]:::di
        DI_Autofac["Autofac RegistrationExtensions"]:::di
        DI_Unity["Unity RegistrationExtensions"]:::di
    end

    %% Core API
    subgraph "Core API Layer"
        API["SharpRedis API (Redis)"]:::core
    end

    %% Commands
    subgraph "Command Module Layer"
        Commands["Command Modules"]:::core
    end

    %% Provider
    subgraph "Provider Layer"
        ProviderCalls["Provider Calls"]:::provider
        ProviderStandard["Provider Standard Base"]:::provider
        ProviderTypes["Provider Type Implementations"]:::provider
    end

    %% Network
    subgraph "Network Layer"
        Interfaces["Network Interfaces"]:::network
        Pool["Connection Pool Implementations"]:::network
        ConnDefault["DefaultConnection"]:::network
        ConnSub["SubConnection"]:::network
    end

    %% Models & Values
    subgraph "Models & Values"
        Models["Models"]:::models
        Values["Value Wrappers"]:::models
        Exceptions["Exceptions"]:::models
    end

    %% Extensions
    Extensions["Extensions"]:::core

    %% External
    External["Redis Server(s)"]:::external

    %% Flow relationships
    Consumer -->|"calls"| API
    Consumer -->|"uses"| DI_Default
    Consumer -->|"uses"| DI_Keyed
    Consumer -->|"uses"| DI_Autofac
    Consumer -->|"uses"| DI_Unity

    DI_Default -->|"registers"| API
    DI_Keyed -->|"registers"| API
    DI_Autofac -->|"registers"| API
    DI_Unity -->|"registers"| API

    API -->|"invokes"| Commands
    Commands -->|"executes"| ProviderCalls
    ProviderCalls -->|"uses base"| ProviderStandard
    ProviderCalls -->|"handles types"| ProviderTypes

    ProviderCalls -->|"requests"| Pool
    Pool -->|"allocates"| ConnDefault
    Pool -->|"allocates"| ConnSub
    Interfaces -->|"defines"| ConnDefault
    Interfaces -->|"defines"| ConnSub
    Interfaces -->|"defines"| Pool

    ConnDefault -->|"TCP"| External
    ConnSub -->|"TCP"| External
    External -->|"response"| ConnDefault
    ConnDefault -->|"returns"| ProviderCalls

    ProviderCalls -->|"parses"| Models
    ProviderCalls -->|"wraps"| Values
    ProviderCalls -->|"throws"| Exceptions

    %% Extensions hooks
    Extensions -.->|"intercepts"| ProviderCalls

    %% Return path
    ProviderCalls -->|"returns"| Commands
    Commands -->|"returns"| API
    API -->|"returns"| Consumer

    %% Click Events
    click Consumer "https://github.com/q7164518/sharpredis/tree/main/examples/"
    click DI_Default "https://github.com/q7164518/sharpredis/blob/main/src/SharpRedis.DependencyInjection/RegistrationExtensions.cs"
    click DI_Keyed "https://github.com/q7164518/sharpredis/blob/main/src/SharpRedis.DependencyInjectionKeyedService/RegistrationExtensions.cs"
    click DI_Autofac "https://github.com/q7164518/sharpredis/blob/main/src/SharpRedis.Autofac/RegistrationExtensions.cs"
    click DI_Unity "https://github.com/q7164518/sharpredis/blob/main/src/SharpRedis.Unity/RegistrationExtensions.cs"
    click API "https://github.com/q7164518/sharpredis/blob/main/src/SharpRedis/Provider/Redis.cs"
    click Commands "https://github.com/q7164518/sharpredis/tree/main/src/SharpRedis/Commands/"
    click ProviderCalls "https://github.com/q7164518/sharpredis/tree/main/src/SharpRedis/Provider/Calls/"
    click ProviderStandard "https://github.com/q7164518/sharpredis/tree/main/src/SharpRedis/Provider/Standard/"
    click ProviderTypes "https://github.com/q7164518/sharpredis/tree/main/src/SharpRedis/Provider/Types/"
    click ConnDefault "https://github.com/q7164518/sharpredis/blob/main/src/SharpRedis/Network/DefaultConnection.cs"
    click ConnSub "https://github.com/q7164518/sharpredis/blob/main/src/SharpRedis/Network/SubConnection.cs"
    click Pool "https://github.com/q7164518/sharpredis/tree/main/src/SharpRedis/Network/Pool/"
    click Interfaces "https://github.com/q7164518/sharpredis/blob/main/src/SharpRedis/Network/Standard/IConnection.cs"
    click Models "https://github.com/q7164518/sharpredis/tree/main/src/SharpRedis/Models/"
    click Values "https://github.com/q7164518/sharpredis/tree/main/src/SharpRedis/Values/"
    click Exceptions "https://github.com/q7164518/sharpredis/tree/main/src/SharpRedis/Exceptions/"

    %% Styles
    classDef consumer fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef di fill:#dddddd,stroke:#333,stroke-width:1px
    classDef core fill:#b3cde0,stroke:#333,stroke-width:1px
    classDef provider fill:#fbb4ae,stroke:#333,stroke-width:1px
    classDef network fill:#ffffb3,stroke:#333,stroke-width:1px
    classDef models fill:#d9d9d9,stroke:#333,stroke-width:1px
    classDef external fill:#fbb4ae,stroke:#333,stroke-width:1px
```
