SmartDapper.Repository 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SmartDapper.Repository --version 1.0.0
                    
NuGet\Install-Package SmartDapper.Repository -Version 1.0.0
                    
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="SmartDapper.Repository" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SmartDapper.Repository" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SmartDapper.Repository" />
                    
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 SmartDapper.Repository --version 1.0.0
                    
#r "nuget: SmartDapper.Repository, 1.0.0"
                    
#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 SmartDapper.Repository@1.0.0
                    
#: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=SmartDapper.Repository&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SmartDapper.Repository&version=1.0.0
                    
Install as a Cake Tool

SmartDapper.Repository

SmartDapper.Repository 是基于 SmartDapper 的 Repository + UnitOfWork 封装,提供一致的 CRUD / 分页 API,并统一事务边界(支持自动提交/回滚与嵌套事务复用),用于组织数据访问层代码。

功能特性

  • 泛型仓储IRepository<T> + Repository<T>(内置常用 CRUD/分页)
  • 工作单元IUnitOfWork + UnitOfWork(统一连接与事务)
  • 事务作用域ITransactionScope + TransactionScope(未 Complete() 自动回滚;支持复用外层事务)
  • 事务快捷封装ExecuteInTransaction* 扩展方法,一行包裹事务边界

安装

dotnet add package SmartDapper.Repository

快速开始(ASP.NET Core)

方式 A:只使用 Repository + UnitOfWork(不依赖中间件)

Program.cs 统一注册 IDbConnection / IUnitOfWork / IRepository<>(推荐):

using System.Data;
using Microsoft.Data.SqlClient;
using SmartDapper.Repository.Extensions;

var builder = WebApplication.CreateBuilder(args);

var connectionString = builder.Configuration.GetConnectionString("Default")!;

builder.Services.AddSmartDapperRepository(
    connectionString,
    cs => new SqlConnection(cs));

提示:如果不是 SQL Server,请自行替换连接类型,或实现你自己的 connectionFactory

方式 A2(推荐):配合 AddSmartDapperConnections(多连接工厂)

如果你已经使用 SmartDapper 的多连接工厂(AddSmartDapperConnections),可以直接让 Repository 复用工厂配置:

using Microsoft.Data.SqlClient;
using SmartDapper.Extensions;
using SmartDapper.Repository.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSmartDapperConnections(o =>
{
    o.Add("Main", builder.Configuration.GetConnectionString("Main")!, cs => new SqlConnection(cs));
    o.DefaultKey = "Main";
});

// 默认连接(CreateDefault)
builder.Services.AddSmartDapperRepository();

// 或指定 key(Create("Main"))
// builder.Services.AddSmartDapperRepository("Main");
方式 B:使用 SmartDapper.Middleware(包含 DI 注册 + 声明式事务)

如果你的项目是 Web API,并希望通过 [UnitOfWork] 自动开启事务,推荐直接用中间件包:

using SmartDapper.Middleware.Extensions;

var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("Default")!;

// 注册仓储 + 工作单元
builder.Services.AddDapperPro(connectionString);

var app = builder.Build();

// 注册工作单元中间件(可选:配合 [UnitOfWork])
app.UseDapperProUnitOfWork();

app.MapControllers();
app.Run();

基础用法

public class CustomerService
{
    private readonly IRepository<Customer> _repo;

    public CustomerService(IRepository<Customer> repo)
    {
        _repo = repo;
    }

    public async Task<long> CreateAsync(Customer customer)
    {
        // 插入并返回自增 ID(如果你的实体/表是自增主键)
        return await _repo.InsertAndGetIdAsync(customer);
    }

    public Task<List<Customer>> ListAsync()
        => _repo.GetAllListAsync();
}

事务用法(推荐)

推荐 1:扩展方法 ExecuteInTransaction*(最简洁)
using SmartDapper.Repository.Extensions;

await _unitOfWork.ExecuteInTransactionAsync(async () =>
{
    await _customerRepository.InsertAsync(customer);
    await _orderRepository.InsertAsync(order);
});
推荐 2:CreateTransactionScope(可控且支持嵌套复用)
await using var scope = _unitOfWork.CreateTransactionScope();
try
{
    await _customerRepository.InsertAsync(customer);
    await _orderRepository.InsertAsync(order);

    await scope.CompleteAsync(); // 提交
}
catch
{
    // 未 Complete 时:DisposeAsync 自动回滚
    throw;
}

嵌套说明:如果外层已存在事务,TransactionScope 会自动复用外层事务,不会重复开启新事务。

不推荐:直接 BeginTransaction(需要你手动 Commit/Rollback)

如果你确实需要“完全手动控制提交/回滚”,可以使用 BeginTransaction/Commit/Rollback(请务必显式提交/回滚,避免长事务)。

生命周期与资源释放(重要)

  • 典型 Web 场景IUnitOfWork / IDbConnection 建议按 Scoped 注册(每个请求一个实例)。
  • 请求结束释放:DI 容器会调用 Dispose/DisposeAsync;若发现“未结束事务”,UnitOfWork 会进行防御性回滚并释放连接资源。
  • 后台任务/非 HTTP 场景:请手动创建 DI Scope,并在 scope 生命周期内使用同一个 IUnitOfWork

相关文档

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 (1)

Showing the top 1 NuGet packages that depend on SmartDapper.Repository:

Package Downloads
SmartDapper.Middleware

SmartDapper.Middleware 为 ASP.NET Core 提供工作单元中间件与注册扩展,支持通过 [UnitOfWork] 特性自动管理事务边界,并提供清晰的事务生命周期日志,适用于 Web API 场景。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 132 1/29/2026
1.0.0 136 1/9/2026