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