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