SmartSql Documentation

repository·master·Indexed 22 days ago

https://github.com/dotnetcore/smartsql

A high-performance data access framework for .NET featuring MyBatis-like XML SQL management, advanced caching (LRU, FIFO, Redis), and dynamic repository capabilities. SmartSql provides absolute control over SQL while reducing boilerplate through XML filter tags, R/W splitting, and automated ID generators like Snowflake and DbSequence. It includes support for AOP transactions, bulk inserts, and observability via Skywalking monitoring.

Tokens
71.5K
Snippets
151
Records
285
Agent score
74%

What's inside SmartSql

  1. What is SmartSql?

    master

    SmartSql is an open-source .NET library (MIT License) designed for connecting applications to databases. Its primary differentiator is XML-based SQL management, where database queries are stored in dedicated XML files rather than being embedded directly in C# code. This approach allows for a separation of concerns where database administrators can optimize queries independently of application code.

    Core features include:

    • XML SQL Management: Decouples SQL from application logic.
    • Read/Write Splitting: Automatically routes reads to replicas and writes to the master.
    • Built-in Caching: Supports LRU, FIFO, and Redis.
    • Dynamic Repositories: Auto-generates data access interfaces to reduce boilerplate.
    • Transaction Management: Declarative support via attributes.
    • Bulk Insert: Per-database support for high-performance data loading.
    • Data Synchronization: Integration with Kafka and RabbitMQ.
    • Diagnostics: Built-in performance monitoring via events.
  2. What is SmartSql?

    master

    SmartSql is a .NET ORM library inspired by MyBatis that treats SQL as a first-class citizen. Unlike EF Core (which uses LINQ) or Dapper (which uses inline C# strings), SmartSql manages SQL statements in external XML files called SmartSqlMaps. This separation allows for easier DBA review, dynamic SQL construction via XML tags, and reusable SQL maps.

    It is compatible with netstandard2.0 and C# 7.3, supporting both .NET Framework 4.6.1+ and .NET Core/.NET 5+.

  3. Overview of SmartSql features

    master

    SmartSql is a high-performance data access framework that combines the ideas of MyBatis with modern .NET capabilities. It uses XML to manage SQL, allowing developers to maintain absolute control over SQL performance while eliminating complex conditional logic in C# code via XML filter tags.

    Key features include:

    • SQL Management: XML-based SQL with conditional tags.
    • Caching: Support for LRU, FIFO, and Redis.
    • R/W Splitting: Easy configuration for read/write separation.
    • Dynamic Repository: Automatic implementation of repository interfaces.
    • Diagnostics & Monitoring: Built-in support for observability.
    • Advanced Data Handling: Type handlers (e.g., JSON), ID generators (Snowflake, DB Sequence), and Bulk Insert support.
  4. Overview of SmartSql Extensions

    master
    SmartSql uses a modular extension system where additional capabilities are provided via separate NuGet packages. This allows developers to include only the specific functionality required for their project, such as dependency injection, bulk operations, or distributed caching. Extensions plug into core components like SmartSqlBuilder, ISqlMapper, SmartSqlConfig, or the Middleware Pipeline.
  5. Use Dynamic Repository to eliminate CRUD boilerplate

    master

    The SmartSql.DyRepository extension allows you to define C# interfaces that act as data access repositories. At runtime, SmartSql generates a fully-functional implementation using IL emit. This implementation automatically maps interface methods to SQL statements defined in your XML configuration based on naming conventions or explicit annotations.

    // Define your interface
    public interface IUserRepository
    {
        // Maps to XML statement with Id="GetUser"
        [Statement(Id = "GetUser")]
        User GetUser(long id);
    }
    
    // Request the instance from the factory
    var userRepository = repositoryFactory.CreateInstance(typeof(IUserRepository), sqlMapper) as IUserRepository;
    
    // Use it
    var user = userRepository.GetUser(123);
  6. Key Features and Capabilities of SmartSql

    master

    SmartSql provides several built-in capabilities to simplify complex data requirements:

    • Dynamic Queries: Automatically adjusts queries based on provided optional filters.
    • Built-in Caching: Supports memory and Redis to improve load times.
    • Read/Write Splitting: Automatically distributes load by routing reads to replicas and writes to the primary database.
    • Bulk Operations: High-performance bulk insert support for loading large datasets.
    • Event Streaming: Built-in integration with Kafka and RabbitMQ for real-time data synchronization.
    • Dynamic Repository: Automatically generates data access code from simple interface definitions.
  7. Architecture Overview of SmartSql

    master

    SmartSql is a .NET ORM inspired by MyBatis that utilizes XML-managed SQL and a middleware-based execution pipeline. The architecture is divided into three primary layers:

    1. Application Layer: Where your application code and Dynamic Repositories reside.
    2. API Layer: The entry point for developers, consisting of ISqlMapper (the primary interface), SqlMapper (the implementation), and SmartSqlBuilder (the configuration tool).
    3. Middleware Pipeline: A linked-list of IMiddleware components that intercept, transform, and observe every SQL invocation.
    4. Data Access Layer: Handles the actual database interaction via IDbSession and ICommandExecuter.
    5. Storage Layer: Includes the physical databases (Read/Write) and the Cache Provider.

    This layered approach allows you to intercept and modify SQL execution at any stage without changing your core business logic.

  8. Configure SmartSql via XML or Programmatic API

    master

    SmartSql supports two configuration methods that both result in the same SmartSqlConfig object at runtime:

    1. XML Configuration: The primary and most common method using a SmartSqlMapConfig.xml file.
    2. Programmatic Configuration: Using the SmartSqlBuilder fluent API.

    The XML approach is preferred for its structure and ease of management in large projects.

    <?xml version="1.0" encoding="utf-8" ?>
    <SmartSqlMapConfig xmlns="http://SmartSql.net/schemas/SmartSqlMapConfig.xsd">
      <Settings />
      <Properties />
      <Database />
      <TypeHandlers />
      <TagBuilders />
      <IdGenerators />
      <SmartSqlMaps />
    </SmartSqlMapConfig>
  9. Explore SmartSql extension categories

    master

    SmartSql provides extensions across several functional areas to enhance data access and system management:

    • Data Access Extensions: Specialized tools for interacting with data stores.
    • Configuration & DI Extensions: Helpers for integrating with Dependency Injection and managing settings.
    • Caching & Synchronization Extensions: Built upon core cache abstractions to manage data consistency (e.g., Redis, Cache Sync).
    • Data Synchronization Extensions: Tools for keeping data in sync across different layers or systems.
    • Dynamic SQL Extensions: Enhancements for generating and managing dynamic SQL queries.
  10. High-performance bulk data loading with SmartSql.Bulk

    master

    The SmartSql.Bulk package provides a database-agnostic interface for high-performance bulk inserts. It uses native database mechanisms to achieve maximum throughput instead of row-by-row insertion.

    Supported database providers include:

    • SQL Server / MsSqlServer: Uses SqlBulkCopy.
    • MySQL / MySqlConnector: Uses MySqlBulkLoader via temporary CSV files.
    • PostgreSQL: Uses COPY BINARY via NpgsqlConnection.BeginBinaryImport().

    All implementations implement the IBulkInsert interface, which supports both synchronous Insert() and asynchronous InsertAsync() methods.

  11. Understand the SmartSql Solution Structure

    master

    SmartSql is modularized into several specialized packages. Depending on your requirements, you may need to install specific extensions for dependency injection, caching, or database-specific bulk operations.

    Core and Integration

    • SmartSql: The core library (netstandard2.0).
    • SmartSql.DyRepository: Enables dynamic repository proxy generation using IL emit.
    • SmartSql.DIExtension: Provides ASP.NET Core integration via services.AddSmartSql().
    • SmartSql.Options: Supports the Options pattern for configuration via appsettings.json.

    Specialized Features

    • SmartSql.Cache.Redis: Adds Redis support for distributed caching.
    • SmartSql.Cache.Sync: Enables cache synchronization across multiple instances.
    • SmartSql.TypeHandler: Provides handlers for JSON and other custom types.
    • SmartSql.AOP: Provides Aspect-Oriented Programming support for transactions using the [Transaction] attribute.
    • SmartSql.Bulk.*: Provides bulk insert capabilities for SqlServer, MySql, or PostgreSql.
    • SmartSql.InvokeSync.*: Facilitates data synchronization via Kafka or RabbitMQ.
  12. SmartSql Extension Categories

    master

    Extensions are categorized by their functional area:

    Data Access

    • Dynamic Repository: Auto-generates repository implementations from interfaces using IL emit.
    • Bulk Insert: High-performance loading using native APIs (e.g., SqlBulkCopy, MySqlBulkLoader).
    • Type Handlers: Custom serialization for JSON, XML, Crypto, and PostgreSQL types.
    • Oracle Support: Configuration for OracleCommand compatibility.

    Configuration & DI

    • DI Integration: Registers SmartSqlBuilder, ISqlMapper, and dynamic repositories into ASP.NET Core.
    • Options Pattern: Configuration via appsettings.json using IOptions<SmartSqlConfigOptions>.
    • AOP Transactions: Declarative transactions using [Transaction] attributes via AspectCore.

    Caching & Synchronization

    • Redis Cache: Shared query result caching across instances.
    • Cache Sync: Uses message queues to synchronize/flush local caches across instances.
    • InvokeSync: Replicates SQL operations to Kafka or RabbitMQ for downstream consumption.

    Dynamic SQL

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