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 SqlSugar;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace zzz.Repository.UnitOfWork
|
|
|
|
|
{
|
|
|
|
|
public class UnitOfWork: IUnitOfWork
|
|
|
|
|
{
|
|
|
|
|
private readonly ISqlSugarClient _sqlSugarClient;
|
|
|
|
|
|
|
|
|
|
private int _tranCount { get; set; }
|
|
|
|
|
|
|
|
|
|
public UnitOfWork(ISqlSugarClient sqlSugarClient)
|
|
|
|
|
{
|
|
|
|
|
_sqlSugarClient = sqlSugarClient;
|
|
|
|
|
_tranCount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取DB,保证唯一性
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public SqlSugarScope GetDbClient()
|
|
|
|
|
{
|
|
|
|
|
// 必须要as,后边会用到切换数据库操作
|
|
|
|
|
// 因为注入的是 SqlSugarScope 所以这里可以转化成功
|
|
|
|
|
return _sqlSugarClient as SqlSugarScope;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void BeginTran()
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
_tranCount++;
|
|
|
|
|
GetDbClient().BeginTran();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CommitTran()
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
_tranCount--;
|
|
|
|
|
if (_tranCount == 0)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
GetDbClient().CommitTran();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
GetDbClient().RollbackTran();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RollbackTran()
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
_tranCount--;
|
|
|
|
|
GetDbClient().RollbackTran();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|