mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
- 增加 UnitOfWorkManager 工作单元管理器,实现多种传播事务;#289
This commit is contained in:
51
Examples/aspnetcore_transaction/TransactionalAttribute.cs
Normal file
51
Examples/aspnetcore_transaction/TransactionalAttribute.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using FreeSql;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用事务执行,请查看 Program.cs 代码开启动态代理
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class TransactionalAttribute : DynamicProxyAttribute, IActionFilter
|
||||
{
|
||||
public Propagation Propagation { get; set; } = Propagation.Requierd;
|
||||
public IsolationLevel? IsolationLevel { get; set; }
|
||||
|
||||
[DynamicProxyFromServices]
|
||||
UnitOfWorkManager _uowManager;
|
||||
IUnitOfWork _uow;
|
||||
|
||||
public override Task Before(DynamicProxyBeforeArguments args) => OnBefore(_uowManager);
|
||||
public override Task After(DynamicProxyAfterArguments args) => OnAfter(args.Exception);
|
||||
|
||||
//这里是为了 controller
|
||||
public void OnActionExecuting(ActionExecutingContext context) => OnBefore(context.HttpContext.RequestServices.GetService(typeof(UnitOfWorkManager)) as UnitOfWorkManager);
|
||||
public void OnActionExecuted(ActionExecutedContext context) => OnAfter(context.Exception);
|
||||
|
||||
|
||||
Task OnBefore(UnitOfWorkManager uowm)
|
||||
{
|
||||
_uow = uowm.Begin(this.Propagation, this.IsolationLevel);
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
Task OnAfter(Exception ex)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ex == null) _uow.Commit();
|
||||
else _uow.Rollback();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_uow.Dispose();
|
||||
}
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user