Skip to content

Extensions Overview

SmartSql provides a modular extension system that allows you to add capabilities beyond the core ORM functionality. Each extension is distributed as a separate NuGet package, so you only pull in what your project actually needs. Extensions range from ASP.NET Core DI integration and dynamic repository proxies to bulk insert providers, distributed cache synchronization, and message-queue-based data replication.

At a Glance

PackagePurposeKey File
SmartSql.DyRepositoryDynamic repository proxy generation via IL emitEmitRepositoryBuilder.cs
SmartSql.DIExtensionASP.NET Core dependency injection integrationSmartSqlDIExtensions.cs
SmartSql.OptionsOptions-pattern configuration from appsettings.jsonSmartSqlConfigOptions.cs
SmartSql.AOPAOP-based transaction management via AspectCoreTransactionAttribute.cs
SmartSql.BulkBulk insert abstraction and base classesIBulkInsert.cs
SmartSql.Bulk.SqlServerSqlServer bulk insert via SqlBulkCopyBulkInsert.cs
SmartSql.Bulk.MsSqlServerMsSqlServer bulk insert via Microsoft.Data.SqlClientBulkInsert.cs
SmartSql.Bulk.MySqlMySQL bulk insert via MySqlBulkLoaderBulkInsert.cs
SmartSql.Bulk.MySqlConnectorMySQL bulk insert via MySqlConnector driverBulkInsert.cs
SmartSql.Bulk.PostgreSqlPostgreSQL bulk insert via NpgsqlConnection.BeginBinaryImportBulkInsert.cs
SmartSql.TypeHandlerJSON, XML, and Crypto type handlersJsonTypeHandler.cs
SmartSql.TypeHandler.PostgreSqlPostgreSQL-specific type handlers (arrays, geometric types)JsonTypeHandler.cs
SmartSql.Cache.RedisRedis-backed cache providerRedisCacheProvider.cs
SmartSql.Cache.SyncDistributed cache synchronization via pub/subSyncCacheManager.cs
SmartSql.InvokeSyncData synchronization abstraction via message queuesSyncService.cs
SmartSql.InvokeSync.KafkaKafka-based invoke synchronizationKafkaPublisher.cs
SmartSql.InvokeSync.RabbitMQRabbitMQ-based invoke synchronizationRabbitMQPublisher.cs
SmartSql.ScriptTagScript tag for JavaScript-based dynamic SQL conditionsScript.cs
SmartSql.OracleOracle DB provider supportSmartSqlBuilderExtensions.cs

Extension Architecture

The following diagram shows how the extension packages relate to the SmartSql core library and to each other. The core (SmartSql) provides the fundamental abstractions, while extensions plug in at specific points.

mermaid
graph TB
    subgraph Extensions["Extensions"]
        style Extensions fill:#161b22,stroke:#30363d,color:#e6edf3
        DyRepo["SmartSql.DyRepository"]
        DI["SmartSql.DIExtension"]
        Options["SmartSql.Options"]
        AOP["SmartSql.AOP"]
        Bulk["SmartSql.Bulk.*"]
        TypeH["SmartSql.TypeHandler"]
        TypeHPg["SmartSql.TypeHandler.PostgreSql"]
        CacheRedis["SmartSql.Cache.Redis"]
        CacheSync["SmartSql.Cache.Sync"]
        InvokeSync["SmartSql.InvokeSync"]
        InvKafka["SmartSql.InvokeSync.Kafka"]
        InvRabbit["SmartSql.InvokeSync.RabbitMQ"]
        ScriptTag["SmartSql.ScriptTag"]
        Oracle["SmartSql.Oracle"]
    end

    subgraph Core["SmartSql Core"]
        style Core fill:#161b22,stroke:#30363d,color:#e6edf3
        SmartSqlBuilder["SmartSqlBuilder"]
        SqlMapper["ISqlMapper"]
        Pipeline["Middleware Pipeline"]
        Cache["Cache System"]
        Config["SmartSqlConfig"]
    end

    DI --> DyRepo
    DI --> SmartSqlBuilder
    Options --> SmartSqlBuilder
    DyRepo --> SqlMapper
    AOP --> SmartSqlBuilder
    Bulk --> SqlMapper
    TypeH --> Config
    TypeHPg --> Config
    CacheRedis --> Cache
    CacheSync --> InvokeSync
    CacheSync --> Cache
    InvokeSync --> Pipeline
    InvKafka --> InvokeSync
    InvRabbit --> InvokeSync
    ScriptTag --> Config
    Oracle --> SmartSqlBuilder

    style DyRepo fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style DI fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style Options fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style AOP fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style Bulk fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style TypeH fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style TypeHPg fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style CacheRedis fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style CacheSync fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style InvokeSync fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style InvKafka fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style InvRabbit fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style ScriptTag fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style Oracle fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style SmartSqlBuilder fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style SqlMapper fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style Pipeline fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style Cache fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style Config fill:#2d333b,stroke:#6d5dfc,color:#e6edf3

Extension Registration Lifecycle

The following sequence diagram illustrates how extensions register themselves during application startup:

mermaid
sequenceDiagram
autonumber
    participant App as Startup
    participant DI as IServiceCollection
    participant SB as SmartSqlBuilder
    participant Config as SmartSqlConfig

    App->>DI: services.AddSmartSql()
    DI->>SB: Build SmartSqlBuilder
    App->>DI: .AddRepositoryFromAssembly()
    App->>DI: .AddInvokeSync()
    App->>SB: builder.UseOracleCommandExecuter()
    App->>SB: builder.UseOptions(sp)
    SB->>Config: Build database, type handlers, caches
    SB->>Config: Load XML SqlMaps
    SB->>Config: Register middleware pipeline
    SB->>SB: Register in SmartSqlContainer

Extension Point Summary

mermaid
classDiagram
    class SmartSqlBuilder {
        +UseXmlConfig() SmartSqlBuilder
        +UseDataSource() SmartSqlBuilder
        +UseCache() SmartSqlBuilder
        +AddTypeHandler() SmartSqlBuilder
        +AddFilter() SmartSqlBuilder
        +AddMiddleware() SmartSqlBuilder
        +UseCommandExecuter() SmartSqlBuilder
        +UseCacheManager() SmartSqlBuilder
        +Build() SmartSqlBuilder
    }

    class IBulkInsert {
        <<interface>>
        +Insert()
        +InsertAsync()
    }

    class ICacheProvider {
        <<interface>>
        +TryAdd() bool
        +TryGetValue() bool
        +Flush()
    }

    class IPublisher {
        <<interface>>
        +PublishAsync(SyncRequest)
    }

    class ISubscriber {
        <<interface>>
        +Received event
        +Start()
        +Stop()
    }

    SmartSqlBuilder --> IBulkInsert : creates
    SmartSqlBuilder --> ICacheProvider : registers
    SmartSqlBuilder --> IPublisher : registers
    SmartSqlBuilder --> ISubscriber : registers

    style SmartSqlBuilder fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style IBulkInsert fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style ICacheProvider fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style IPublisher fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style ISubscriber fill:#2d333b,stroke:#6d5dfc,color:#e6edf3

Extension Categories

Data Access Extensions

These extensions enhance how SmartSql interacts with databases and maps data:

  • Dynamic Repository -- Eliminates boilerplate repository code by auto-generating implementations from interface definitions at runtime using IL emit.
  • Bulk Insert -- High-performance bulk data loading using database-specific native APIs (SqlBulkCopy, MySqlBulkLoader, COPY BINARY).
  • Type Handlers -- Custom serialization for JSON, XML, cryptography, and PostgreSQL-specific types like arrays and geometric shapes.
  • Oracle Support -- Oracle-specific command executer configuration for OracleCommand compatibility.

Configuration & DI Extensions

These extensions integrate SmartSql with the ASP.NET Core ecosystem:

  • DI Integration -- Registers SmartSqlBuilder, ISqlMapper, and dynamic repositories into the ASP.NET Core service container.
  • Options Pattern -- Configures SmartSql entirely from appsettings.json using IOptions<SmartSqlConfigOptions>.
  • AOP Transactions -- Declarative transaction management via AspectCore [Transaction] interceptor attributes.

Caching & Synchronization Extensions

These extensions extend SmartSql's caching capabilities to distributed environments:

  • Redis Cache -- Stores query result caches in Redis for shared caching across multiple application instances.
  • Cache Sync -- Listens to message queue events to flush local caches when data changes on other instances.

Data Synchronization Extensions

These extensions replicate SQL operations to other systems via message queues:

Dynamic SQL Extensions

  • Script Tag -- Enables JavaScript-based expressions in XML SQL maps for complex conditional logic.

Cross-References

  • See Middleware Pipeline for how extensions plug into the SQL execution flow.
  • See Configuration for XML-based configuration that complements Options pattern.
  • See Caching for the core cache abstractions that Redis and Cache Sync build upon.

References

Released under the MIT License.