YD_Event.Admin/YD_Event.Core/SqlSugar/SqlSugarUnitOfWork.cs
2026-04-12 16:11:20 +08:00

60 lines
1.7 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace SqlSugar;
/// <summary>
/// 数据库事务(工作单元保证数据的一致性)
/// </summary>
public class SqlSugarUnitOfWork : IUnitOfWork
{
private readonly ISqlSugarClient _client;
public SqlSugarUnitOfWork(ISqlSugarClient client)
{
_client = client;
}
/// <summary>
/// 开启事务
/// </summary>
/// <param name="context"></param>
/// <param name="unitOfWork"></param>
public void BeginTransaction(FilterContext context, UnitOfWorkAttribute unitOfWork)
{
_client.Ado.BeginTran();
}
/// <summary>
/// 提交事务
/// </summary>
/// <param name="resultContext"></param>
/// <param name="unitOfWork"></param>
public void CommitTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
{
if (_client.Ado.Transaction != null && _client.Ado.Connection != null && _client.Ado.Connection.State == System.Data.ConnectionState.Open)
{
_client.Ado.CommitTran();
}
}
/// <summary>
/// 回滚
/// </summary>
/// <param name="resultContext"></param>
/// <param name="unitOfWork"></param>
public void RollbackTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
{
if (_client.Ado.Transaction != null && _client.Ado.Connection != null && _client.Ado.Connection.State == System.Data.ConnectionState.Open)
{
_client.Ado.RollbackTran();
}
}
/// <summary>
/// 释放连接对象
/// </summary>
/// <param name="context"></param>
/// <param name="resultContext"></param>
public void OnCompleted(FilterContext context, FilterContext resultContext)
{
_client.Dispose();
}
}