Galosys.Foundation.Dapper 26.10.29.1

dotnet add package Galosys.Foundation.Dapper --version 26.10.29.1
                    
NuGet\Install-Package Galosys.Foundation.Dapper -Version 26.10.29.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="Galosys.Foundation.Dapper" Version="26.10.29.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.Dapper" Version="26.10.29.1" />
                    
Directory.Packages.props
<PackageReference Include="Galosys.Foundation.Dapper" />
                    
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 Galosys.Foundation.Dapper --version 26.10.29.1
                    
#r "nuget: Galosys.Foundation.Dapper, 26.10.29.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 Galosys.Foundation.Dapper@26.10.29.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=Galosys.Foundation.Dapper&version=26.10.29.1
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.Dapper&version=26.10.29.1
                    
Install as a Cake Tool

Galosys.Foundation.Dapper

成熟度: 🟢 稳定 — 生产可用,测试充分,活跃维护

简介

Galosys.Foundation.Dapper 基于 Dapper 提供轻量级 ORM 和数据访问支持,包含工作单元模式、SQL 映射、CDC(变更数据捕获)等功能。

特性

  • Dapper 集成 - 高性能轻量级 ORM
  • 工作单元模式 (UnitOfWork) - 事务管理
  • SQL 映射 - 静态 SQL 查询映射
  • SQL 构建器 - 动态 SQL 构建
  • CDC 监听器 - 变更数据捕获(支持 SQL Server)
  • 支持多种数据库 - SQL Server, Oracle 等

安装

dotnet add package Galosys.Foundation.Dapper

使用

工作单元

public class OrderRepository
{
    private readonly IUnitOfWork _unitOfWork;

    public OrderRepository(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public async Task InsertOrderAsync(Order order)
    {
        var sql = "INSERT INTO Orders (Id, OrderNo, Amount) VALUES (@Id, @OrderNo, @Amount)";
        await _unitOfWork.ExecuteAsync(sql, order);
    }

    public async Task CommitAsync()
    {
        await _unitOfWork.CommitAsync();
    }
}

查询示例

public class UserService
{
    private readonly IDbConnection _db;

    public UserService(IDbConnection db)
    {
        _db = db;
    }

    public async Task<IEnumerable<User>> GetUsersAsync()
    {
        var sql = "SELECT * FROM Users WHERE Deleted = 0";
        return await _db.QueryAsync<User>(sql);
    }

    public async Task<User> GetUserByIdAsync(long id)
    {
        var sql = "SELECT * FROM Users WHERE Id = @Id";
        return await _db.QueryFirstOrDefaultAsync<User>(sql, new { Id = id });
    }

    public async Task<int> InsertUserAsync(User user)
    {
        var sql = @"INSERT INTO Users (Name, Email) VALUES (@Name, @Email);
                    SELECT CAST(SCOPE_IDENTITY() as bigint)";
        return await _db.ExecuteScalarAsync<int>(sql, user);
    }
}

SQL 映射

// 使用 SqlMap 进行静态 SQL 映射
var result = await SqlMap.QueryAsync<User>("SelectUsers");

CDC 监听(变更数据捕获)

public class OrderCdcListener : ICdcListener
{
    public DatabaseType DatabaseType => DatabaseType.SqlServer;

    public string[] TableNames => new[] { "Orders" };

    public async Task HandleAsync(CdcEventArgs args)
    {
        // 处理变更数据
        Console.WriteLine($"Operation: {args.Operation}");
        Console.WriteLine($"Data: {args.Data}");
    }
}

// 注册 CDC 监听器
services.AddCdcListener<OrderCdcListener>();

分页查询

public async Task<PageOutput> GetUserPageAsync(int page, int size)
{
    var sql = "SELECT * FROM Users ORDER BY Id OFFSET @Offset ROWS FETCH NEXT @Size ROWS ONLY";
    var countSql = "SELECT COUNT(*) FROM Users";
    
    var items = await _db.QueryAsync<User>(sql, new { Offset = (page - 1) * size, Size = size });
    var total = await _db.ExecuteScalarAsync<int>(countSql);
    
    return PageOutput.Of(items, total, page, size);
}

模块化异常处理

Dapper 模块提供 MySqlExceptionHandler(绑定 MySqlConnector.MySqlException,[ExceptionHandler(200)]),用于在 MySql 驱动抛错时优先于框架默认 DbExceptionHandler(0) 接管。处理器归属 Dapper 而非 Data 的原因:MySqlConnector 依赖仅在 Dapper(直引)与 EFCore(pomelo 传递)出现,避免 Data 模块背上 MySql 依赖。

[ExceptionHandler(200)]
public sealed class MySqlExceptionHandler : TypedExceptionHandler<MySqlConnector.MySqlException>
{
    public override ValueTask<ExceptionHandlingOutput> HandleAsync(
        MySqlConnector.MySqlException exception,
        CancellationToken cancellationToken = default)
    {
        var (code, message) = Map(exception.Number);
        return ValueTask.FromResult(new ExceptionHandlingOutput(400, code, message));
    }

    private static (string code, string message) Map(int number) => number switch
    {
        1062 => ("A00400", "提示:数据库主键冲突,请联系管理员"),
        1452 => ("A00400", "提示:违反外键约束,请联系管理员"),
        _ => (ErrorCode.Of("A", (short)BizErrorCode.ParameterInvalid),
              "提示:数据库操作异常,请联系管理员"),
    };
}

处理器继承 System.TypedExceptionHandler<T> 并标注 [ExceptionHandler(200)],经 AddAnnotationConfiguration 自动注册;按 Priority 降序分发时优先于框架默认 DbExceptionHandler(0),确保 MySql 错误码(主键冲突 1062、外键约束 1452 等)映射到对用户友好的提示文案。

核心类

类 说明
DapperUnitOfWork Dapper 工作单元实现
SqlMap SQL 静态映射
SqlBuilderExtensions SQL 构建扩展
MssqlCdcListener SQL Server CDC 监听器
ICdcListener CDC 监听器接口

依赖

  • Dapper
  • Microsoft.Data.SqlClient (SQL Server)
  • Oracle.ManagedDataAccess.Core (Oracle)
  • Galosys.Foundation.Core
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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.10.29.1 113 10/29/2026
26.10.20.1 103 10/20/2026
26.10.16.2 96 10/16/2026
26.10.16.1 102 10/16/2026
26.10.9.1 103 10/9/2026
26.10.8.1 96 10/8/2026
26.9.23.1 119 9/23/2026
26.9.16.1 112 9/16/2026
26.9.15.1 107 9/15/2026
26.9.14.1 110 9/14/2026
26.9.10.1 123 9/10/2026
26.9.3.1 130 9/3/2026
26.8.29.1 125 8/31/2026
26.8.26.1 127 8/26/2026
26.8.23.1 128 8/23/2026
26.8.21.1 118 8/21/2026
26.8.20.1 121 8/20/2026
26.8.18.1 122 8/18/2026
26.8.17.1 130 8/17/2026
26.8.13.2 121 8/13/2026
Loading failed