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