Skip to content

Configuration API

SmartSql is configured through the SmartSqlBuilder fluent API, which constructs a SmartSqlConfig containing all runtime services. The builder assembles the middleware pipeline, registers type handlers, initializes filters, and sets up caching before producing the final ISqlMapper and IDbSessionFactory instances.

At a Glance

ConceptClassPurpose
BuilderSmartSqlBuilderFluent API to configure and construct the runtime
ConfigSmartSqlConfigRuntime configuration holding all services and mappings
SettingsSettingsGlobal toggle settings (cache, parameter case, etc.)
DatabaseDatabaseDatabase provider and data source configuration
PropertiesPropertiesImport variables for XML ${Property} substitution

SmartSqlBuilder Fluent API

Builder Flow Diagram

mermaid
flowchart TD
    subgraph builder["SmartSqlBuilder Configuration Flow"]
        style builder fill:#161b22,stroke:#30363d,color:#e6edf3
        Start["new SmartSqlBuilder()"] --> Source
        Source["UseXmlConfig() / UseDataSource() / UseNativeConfig()"]
        Source --> Alias["UseAlias()"]
        Alias --> Logger["UseLoggerFactory()"]
        Logger --> Cache["UseCache() / UseCacheManager()"]
        Cache --> Entities["RegisterEntity()"]
        Entities --> TH["AddTypeHandler()"]
        TH --> Filters["AddFilter()"]
        Filters --> MW["AddMiddleware()"]
        MW --> Props["UseProperties() / UsePropertiesFromEnv()"]
        Props --> CUD["UseCUDConfigBuilder()"]
        CUD --> Build["Build()"]
        Build --> Output["ISqlMapper + IDbSessionFactory"]
        Build --> Register["SmartSqlContainer.Register()"]
    end

Configuration Source Methods

Choose one of these as the first call to establish how SmartSql loads its configuration:

MethodDescription
UseXmlConfig(resourceType, path)Loads configuration from an XML file. Default path: SmartSqlMapConfig.xml. resourceType can be File, EmbeddedResource, or Directory.
UseDataSource(writeDataSource)Configures directly with a WriteDataSource object (provider + connection string).
UseDataSource(dbProviderName, connectionString)Shorthand that resolves the provider by name from DbProviderManager.
UseNativeConfig(smartSqlConfig)Uses a pre-built SmartSqlConfig object directly.
csharp
// XML configuration
var builder = new SmartSqlBuilder()
    .UseXmlConfig()
    .Build();

// Code-based configuration
var builder = new SmartSqlBuilder()
    .UseDataSource("MySql", "Server=localhost;Database=SmartSqlTest;Uid=root;Pwd=root;")
    .Build();

Identity and Logging

MethodDefaultDescription
UseAlias(alias)"SmartSql"Sets the instance name. Must be non-empty. Used to identify this instance in the SmartSqlContainer.
UseLoggerFactory(factory)NullLoggerFactorySets the logger factory for all internal logging.

Cache Configuration

MethodDescription
UseCache(isCacheEnabled = true)Enables or disables the caching middleware globally.
UseCacheManager(cacheManager)Provides a custom ICacheManager implementation (e.g., Redis cache).
UseIgnoreDbNull(ignoreDbNull = false)When true, ignores DBNull values when building result objects.

Entity Registration

MethodDescription
RegisterEntity(entityType)Registers a single entity type for metadata cache initialization and CUD builder support.
RegisterEntity(typeScanOptions)Scans assemblies by TypeScanOptions and registers all matching entity types.

Type Handler and Deserializer Registration

MethodDescription
AddTypeHandler(typeHandler)Registers a custom TypeHandler for parameter/property conversion.
AddDeserializer(deserializer)Registers a custom IDataReaderDeserializer for result mapping.

Filter and Middleware Registration

MethodDescription
AddFilter<TFilter>()Adds a filter by type (must implement IFilter with a parameterless constructor).
AddFilter(filter)Adds a filter instance.
AddMiddleware(middleware)Adds a custom IMiddleware to the pipeline (inserted after built-in middlewares). See Middleware API.

Properties and Environment

MethodDescription
UseProperties(kvp)Imports key-value properties for XML ${Property} substitution.
UseProperties(dictionary)Imports from an IDictionary.
UsePropertiesFromEnv(target)Imports from system environment variables.

Advanced Options

MethodDescription
UseCUDConfigBuilder()Enables auto-generated CUD (Create/Update/Delete) SQL statements from registered entities.
UseCommandExecuter(executer)Provides a custom ICommandExecuter implementation.
UseDataSourceFilter(filter)Provides a custom IDataSourceFilter for read/write splitting logic.
RegisterToContainer(registered = false)When false, prevents auto-registration in SmartSqlContainer.
ListenInvokeSucceeded(callback)Registers a callback for every successful command execution.

Build Method

MethodDescription
Build()Constructs the runtime. Idempotent -- calling it again returns immediately. Internally: configures SmartSqlConfig, builds the middleware pipeline, initializes deserializers, registers type handlers, and registers in SmartSqlContainer.
csharp
var builder = new SmartSqlBuilder()
    .UseAlias("MyApp")
    .UseXmlConfig()
    .UseCache()
    .RegisterEntity<User>()
    .AddFilter<CustomFilter>()
    .UseLoggerFactory(loggerFactory)
    .Build();

ISqlMapper mapper = builder.GetSqlMapper();
IDbSessionFactory factory = builder.GetDbSessionFactory();

SmartSqlConfig

The central runtime configuration object. Constructed by SmartSqlBuilder.Build() and held by all runtime services.

Properties

PropertyTypeDescription
AliasstringInstance identifier
SettingsSettingsGlobal toggle settings
DatabaseDatabaseDatabase provider and data sources
PropertiesPropertiesImport variables
SqlMapsIDictionary<string, SqlMap>All loaded SQL map scopes
LoggerFactoryILoggerFactoryLogging factory
ObjectFactoryBuilderIObjectFactoryBuilderFactory for creating object instances (default: ExpressionObjectFactoryBuilder)
DeserializerFactoryIDeserializerFactoryChain of IDataReaderDeserializer instances
TypeHandlerFactoryTypeHandlerFactoryRegistry of type handlers
TagBuilderFactoryITagBuilderFactoryFactory for XML dynamic tag builders
StatementAnalyzerStatementAnalyzerParses statement XML
SqlParamAnalyzerSqlParamAnalyzerAnalyzes SQL parameter placeholders
CacheTemplateAnalyzerSqlParamAnalyzerAnalyzes cache template parameters
PipelineIMiddlewareThe head of the middleware linked list
DataSourceFilterIDataSourceFilterRead/write data source selection
SessionStoreIDbSessionStoreThread-local session management
DbSessionFactoryIDbSessionFactorySession factory
CacheManagerICacheManagerCache manager
CommandExecuterICommandExecuterExecutes DbCommand objects
InvokeSucceedListenerInvokeSucceedListenerEvent listener for successful invocations
IdGeneratorsIDictionary<string, IIdGenerator>ID generators (default: SnowflakeId)
AutoConvertersIDictionary<string, IAutoConverter>Named auto-converters
DefaultAutoConverterIAutoConverterDefault auto-converter (default: NoneAutoConverter)
FiltersFilterCollectionRegistered filters

Lookup Methods

MethodDescription
GetSqlMap(scope)Returns a SqlMap by scope name. Throws if not found.
GetStatement(fullId)Returns a Statement by full ID (e.g., "User.Query").
GetCache(fullId)Returns a Cache by full ID.
GetResultMap(fullId)Returns a ResultMap by full ID.
GetParameterMap(fullId)Returns a ParameterMap by full ID.
GetMultipleResultMap(fullId)Returns a MultipleResultMap by full ID.

Settings

Global toggle settings with sensible defaults:

SettingDefaultDescription
IgnoreParameterCasefalseWhen true, parameter names are case-insensitive
IsCacheEnabledfalseGlobal cache toggle
ParameterPrefix"$"Prefix for parameter placeholders in XML
EnablePropertyChangedTrackfalseEnables property change tracking for entities
IgnoreDbNullfalseWhen true, DBNull values are skipped during result mapping

Build Process Internals

The Build() method performs these steps in order:

mermaid
flowchart TD
    subgraph build["SmartSqlBuilder.Build() Internals"]
        style build fill:#161b22,stroke:#30363d,color:#e6edf3
        A["BeforeBuildInitService()"] --> B["Create RootConfigBuilder"]
        B --> C["Apply CUD config if enabled"]
        C --> D["ConfigBuilder.Build() -> SmartSqlConfig"]
        D --> E["Set Alias, LoggerFactory"]
        E --> F["Init DataSourceFilter, CommandExecuter"]
        F --> G["Apply Cache, IgnoreDbNull settings"]
        G --> H["Init InvokeSucceedListener"]
        H --> I["Init SqlParamAnalyzer, CacheTemplateAnalyzer"]
        I --> J["InitDeserializerFactory()"]
        J --> K["InitFilters()"]
        K --> L["InitTypeHandlers()"]
        L --> M["BuildPipeline()"]
        M --> N["Create SqlMapper"]
        N --> O["Register in SmartSqlContainer"]
        O --> P["BuildNamedTypeHandlerCache"]
        P --> Q["SetupSmartSql() on all components"]
        Q --> R["InitEntityMetaDataCache()"]
    end

Deserializer Chain Initialization

The deserializer chain is initialized in a specific order. Custom deserializers registered via AddDeserializer() are inserted into this chain:

OrderDeserializerPurpose
1MultipleResultDeserializerHandles multiple result sets
2ValueTupleDeserializerMaps to ValueTuple types
3ValueTypeDeserializerMaps to value types (int, string, etc.)
4DynamicDeserializerMaps to dynamic / ExpandoObject
5EntityDeserializerMaps to POCO entities (always last)

Pipeline Construction

When IsCacheEnabled is true, the CachingMiddleware is inserted between PrepareStatementMiddleware and TransactionMiddleware. When false, a NoneCacheManager is used and CachingMiddleware is omitted entirely. Custom middlewares added via AddMiddleware() are appended after all built-in middlewares.

mermaid
flowchart LR
    subgraph cached["Pipeline with Cache Enabled"]
        style cached fill:#161b22,stroke:#30363d,color:#e6edf3
        I["Initializer<br>Order: 0"] --> PS["PrepareStatement<br>Order: 100"]
        PS --> C["Caching<br>Order: 200"]
        C --> T["Transaction<br>Order: 300"]
        T --> DS["DataSourceFilter<br>Order: 400"]
        DS --> CE["CommandExecuter<br>Order: 500"]
        CE --> RH["ResultHandler<br>Order: 600"]
        RH --> Custom["Custom Middleware"]
    end

Cross-References

References

SourceDescription
src/SmartSql/SmartSqlBuilder.csFluent builder with all configuration methods
src/SmartSql/Configuration/SmartSqlConfig.csCentral configuration class
src/SmartSql/SqlMapper.csSqlMapper constructor showing session store usage
src/SmartSql/DbSession/IDbSessionFactory.csSession factory interface
src/SmartSql/Middlewares/AbstractMiddleware.csBase middleware class

Released under the MIT License.