You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
968 B

using HybirdFrameworkCore.Autofac.Attribute;
using SqlSugar;
8 months ago
6 months ago
namespace Repository.UnitOfWork;
[Scope("SingleInstance")]
public class UnitOfWork
8 months ago
{
6 months ago
private readonly ISqlSugarClient _sqlSugarClient;
8 months ago
6 months ago
public UnitOfWork(ISqlSugarClient sqlSugarClient)
{
_sqlSugarClient = sqlSugarClient;
}
8 months ago
6 months ago
/// <summary>
/// 获取DB保证唯一性
/// </summary>
/// <returns></returns>
public SqlSugarScope GetDbClient()
{
// 必须要as后边会用到切换数据库操作
return _sqlSugarClient as SqlSugarScope;
}
8 months ago
6 months ago
public void BeginTran()
{
GetDbClient().BeginTran();
}
8 months ago
6 months ago
public void CommitTran()
{
try
8 months ago
{
6 months ago
GetDbClient().CommitTran(); //
8 months ago
}
6 months ago
catch (Exception ex)
8 months ago
{
GetDbClient().RollbackTran();
6 months ago
throw;
8 months ago
}
}
6 months ago
public void RollbackTran()
{
GetDbClient().RollbackTran();
}
8 months ago
}