Xpandables.Data 10.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Xpandables.Data --version 10.0.1
                    
NuGet\Install-Package Xpandables.Data -Version 10.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Xpandables.Data" Version="10.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Xpandables.Data" Version="10.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Xpandables.Data" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Xpandables.Data --version 10.0.1
                    
#r "nuget: Xpandables.Data, 10.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Xpandables.Data@10.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Xpandables.Data&version=10.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Xpandables.Data&version=10.0.1
                    
Install as a Cake Tool

System.Data

NuGet NuGet Downloads .NET License

ADO.NET repository, unit of work, and SQL builder infrastructure for .NET.

Overview

System.Data provides a lightweight ADO.NET data access layer with Repository and Unit of Work patterns, type-safe query specifications, SQL builders for multiple dialects, and connection management. All operations execute immediately against the database (no change tracking).

Built for .NET 10 with full async support.

Features

Repository Pattern

  • IDataRepository — Base repository marker (implements IDisposable, IAsyncDisposable)
  • IDataRepository<TData> — Generic repository with QueryAsync, InsertAsync, UpdateAsync, DeleteAsync
  • DataRepository<TData> — Default ADO.NET repository implementation
  • IDataRequiresUnitOfWork — Marker for pipeline UoW integration

Query Specifications

  • DataSpecification — Static factory with For<TData>() method
  • IDataSpecification<TData, TResult> — Type-safe query specification

Unit of Work

  • IDataUnitOfWork — Transaction management with GetRepository<T>(), BeginTransactionAsync()
  • DataUnitOfWork — Default ADO.NET unit of work implementation
  • IDataTransaction — Transaction abstraction with CommitAsync(), RollbackAsync()
  • DataTransaction — Default transaction implementation

SQL Builders

  • IDataSqlBuilder — SQL builder interface
  • MsDataSqlBuilder — SQL Server SQL builder
  • PostgreDataSqlBuilder — PostgreSQL SQL builder
  • MyDataSqlBuilder — MySQL SQL builder
  • DataSqlBuilderFactory — Factory for SQL builders
  • DataSqlBuilderBase — Base class for custom builders

Connection Management

  • IDataDbConnectionFactory — Database connection factory
  • DataDbConnectionFactory — Default connection factory
  • IDataDbConnectionScope — Scoped connection wrapper
  • DataDbConnectionScope — Default scoped connection
  • IDataDbConnectionFactoryProvider — Provider for connection factories
  • DataDbConnectionFactoryProvider — Default provider
  • DbProviders — Provider constants (MsSqlServer, PostgreSql, MySql)

SQL Mapping

  • IDataSqlMapper — Maps specifications to SQL
  • DataSqlMapper — Default SQL mapper implementation

Entity Updates

  • DataUpdater — Fluent API for building ADO.NET update operations
  • IDataPropertyUpdate — Property update abstraction

Command Interceptor

  • IDataCommandInterceptor — Intercept command execution for logging, telemetry, and diagnostics
  • DataCommandInterceptor — Base no-op implementation with virtual methods for selective override
  • DataLoggingCommandInterceptor — Default structured logging implementation using [LoggerMessage] source generation
  • DataCommandInterceptorOptions — Options for sensitive data logging, custom category, and slow command threshold
  • DataCommandContext — Context record with SQL text, parameters, operation type, and entity name
  • DataCommandOperationType — Enum: Reader, Scalar, NonQuery

Installation

dotnet add package Xpandables.Data

Quick Start

Register Services

using Microsoft.Extensions.DependencyInjection;

// Register SQL Server connection
services.AddXDataDbConnectionMsSqlServer(connectionString);

// Or PostgreSQL
services.AddXDataDbConnectionPostgreSql(connectionString);

// Or MySQL
services.AddXDataDbConnectionMySql(connectionString);

// Register SQL builder
services.AddXDataMsSqlBuilder();       // SQL Server
services.AddXDataPostgreSqlBuilder();  // PostgreSQL
services.AddXDataMySqlBuilder();       // MySQL

// Register SQL mapper
services.AddXDataSqlMapper();

// Register connection scope
services.AddXDataDbConnectionScope();

// Register unit of work and repositories
services.AddXDataUnitOfWork();
services.AddXDataRepositories(typeof(Program).Assembly);

Use the Repository

using System.Data;

public class OrderService(IDataUnitOfWork unitOfWork)
{
    public async Task CreateOrderAsync(
        Order order, CancellationToken ct)
    {
        await using var transaction = await unitOfWork
            .BeginTransactionAsync(ct);

        try
        {
            var repo = unitOfWork
                .GetRepository<Order>();

            await repo.InsertAsync([order], ct);

            await transaction.CommitAsync(ct);
        }
        catch
        {
            await transaction.RollbackAsync(ct);
            throw;
        }
    }
}

Query with Specifications

using System.Data;

var spec = DataSpecification
    .For<Product>()
    .Where(p => p.IsActive)
    .OrderBy(p => p.Name)
    .Skip(0)
    .Take(20)
    .Select(p => new ProductDto(p.Id, p.Name, p.Price));

await foreach (var product in repository.QueryAsync(spec, ct))
{
    Console.WriteLine(product.Name);
}

Command Interceptor

A DataLoggingCommandInterceptor is registered automatically when you call AddXDataUnitOfWork(). It logs all command execution using structured ILogger output with [LoggerMessage] source generation:

Event Log Level Content
Before execution Debug SQL text, parameter names (or values if enabled)
After execution Information Duration, rows affected, SQL text
Slow command Warning Duration exceeding threshold
Failed execution Error Duration, SQL text, exception
Configure Options
services.AddXDataCommandInterceptor(options =>
{
    // Log parameter values (default: false — only names are logged)
    options.EnableSensitiveDataLogging = true;

    // Custom log category (default: null — uses DataLoggingCommandInterceptor type name)
    options.CategoryName = "MyApp.Database";

    // Commands exceeding this threshold log at Warning (default: null — disabled)
    options.SlowCommandThreshold = TimeSpan.FromSeconds(2);
});

services.AddXDataUnitOfWork(); // safe — TryAdd won't overwrite
Custom Interceptor

To fully replace the logging interceptor with your own implementation:

public sealed class MetricsCommandInterceptor : DataCommandInterceptor
{
    public override ValueTask CommandExecutedAsync(
        DataCommandContext context, TimeSpan duration, int? rowsAffected,
        CancellationToken cancellationToken = default)
    {
        // Push metrics to your observability system
        return ValueTask.CompletedTask;
    }
}

services.AddXDataCommandInterceptor<MetricsCommandInterceptor>();
services.AddXDataUnitOfWork();

Registration behavior:

  • AddXDataUnitOfWork() auto-registers the DataLoggingCommandInterceptor via TryAdd (won't overwrite a previously registered custom interceptor).
  • AddXDataCommandInterceptor(Action?) configures options via the IOptions<T> pipeline and registers the logging interceptor.
  • AddXDataCommandInterceptor<T>() uses Replace to swap the current registration.
  • If you register your custom interceptor before AddXDataUnitOfWork(), it is preserved.

---

## Core Types

| Type | Description |
|------|-------------|
| `IDataRepository<TData>` | Generic ADO.NET repository |
| `IDataUnitOfWork` | ADO.NET transaction coordinator |
| `IDataTransaction` | Transaction with commit/rollback |
| `DataSpecification` | Query specification factory |
| `IDataSqlBuilder` | SQL builder interface |
| `IDataDbConnectionFactory` | Connection factory |
| `DataUpdater` | Fluent update builder |
| `IDataRequiresUnitOfWork` | Pipeline UoW marker |
| `IDataCommandInterceptor` | Command execution interceptor |
| `DataLoggingCommandInterceptor` | Default structured logging interceptor |
| `DataCommandInterceptorOptions` | Interceptor configuration options |

## DI Extension Methods

| Method | Description |
|--------|-------------|
| `AddXDataDbConnectionMsSqlServer(cs)` | Register SQL Server connection |
| `AddXDataDbConnectionPostgreSql(cs)` | Register PostgreSQL connection |
| `AddXDataDbConnectionMySql(cs)` | Register MySQL connection |
| `AddXDataMsSqlBuilder()` | Register SQL Server SQL builder |
| `AddXDataPostgreSqlBuilder()` | Register PostgreSQL SQL builder |
| `AddXDataMySqlBuilder()` | Register MySQL SQL builder |
| `AddXDataSqlMapper()` | Register SQL mapper |
| `AddXDataUnitOfWork()` | Register unit of work (auto-registers logging interceptor) |
| `AddXDataCommandInterceptor()` | Register logging interceptor with default options |
| `AddXDataCommandInterceptor(Action)` | Register logging interceptor with custom options |
| `AddXDataCommandInterceptor<T>()` | Replace interceptor with custom implementation |
| `AddXDataRepositories(assemblies)` | Register repositories from assemblies |
| `AddXDataDbConnectionScope()` | Register scoped connection |

---

## 📚 Related Packages

| Package | Description |
|---------|-------------|
| **Xpandables.Entities** | Entity abstractions with lifecycle tracking |
| **Xpandables.Events.Data** | Event store built on System.Data |

---

## 📄 License

Apache License 2.0 — Copyright © Kamersoft 2025
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Xpandables.Data:

Package Downloads
Xpandables.Events

Event-driven architecture: in-process publish/subscribe, event-sourced aggregates and an event store, outbox and inbox with a hosted scheduler, over Xpandables.Data; Native AOT compatible.

Xpandables.Events.Data

Events ADO.NET implementation.

Xpandables.Results.Pipelines

Pipeline pattern implementation.

Xpandables.AspNetCore.Net

ASP.NET Core minimal API integration of the Xpandables libraries: Result to HTTP responses and problem details, endpoint filters, routes, request context, pipeline behaviors and DI extensions; Native AOT compatible.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
11.0.0 0 11/29/2026
10.0.2 209 3/15/2026
10.0.1 180 2/20/2026