Galosys.Foundation.Dapper 26.5.18.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Galosys.Foundation.Dapper --version 26.5.18.1
                    
NuGet\Install-Package Galosys.Foundation.Dapper -Version 26.5.18.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.5.18.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.5.18.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.5.18.1
                    
#r "nuget: Galosys.Foundation.Dapper, 26.5.18.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.5.18.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.5.18.1
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.Dapper&version=26.5.18.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);
}

核心类

说明
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 net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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.5.19.1 93 5/19/2026
26.5.18.1 92 5/18/2026
26.5.15.1 93 5/15/2026
26.5.12.3 91 5/12/2026
26.5.12.2 89 5/12/2026
26.4.27.1-rc1 92 4/26/2026
26.4.25.1-rc1 88 4/25/2026
26.4.22.2-rc7 103 4/22/2026
26.4.22.2-rc6 86 4/22/2026
26.4.22.2-rc4 86 4/22/2026
26.4.22.2-rc3 88 4/22/2026
26.4.19.1-rc1 94 4/19/2026
26.4.12.8-rc1 99 4/12/2026
26.4.12.7-rc1 91 4/12/2026
26.1.30.1-rc1 121 1/30/2026
26.1.29.1 122 1/29/2026
26.1.28.5 111 1/28/2026
26.1.28.4 108 1/28/2026
26.1.28.2 113 1/28/2026
26.1.23.6 131 1/23/2026
Loading failed