添加工作单元
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Uow.Interceptors
|
||||
{
|
||||
public class UnitOfWorkAttribute : Attribute// : AbstractInterceptorAttribute
|
||||
{
|
||||
public UnitOfWorkAttribute(bool isTransactional = true)
|
||||
{
|
||||
IsTransactional = isTransactional;
|
||||
}
|
||||
public UnitOfWorkAttribute(IsolationLevel isolationLevel, bool isTransactional = true) : this(isTransactional)
|
||||
{
|
||||
IsolationLevel = isolationLevel;
|
||||
}
|
||||
public UnitOfWorkAttribute(IsolationLevel isolationLevel, int timeout, bool isTransactional = true) : this(isolationLevel, isTransactional)
|
||||
{
|
||||
Timeout = timeout;
|
||||
}
|
||||
|
||||
public bool IsTransactional { get; }
|
||||
|
||||
public IsolationLevel? IsolationLevel { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Milliseconds
|
||||
/// </summary>
|
||||
public int? Timeout { get; }
|
||||
public bool IsDisabled { get; }
|
||||
|
||||
|
||||
//public override Task Invoke(AspectContext context, AspectDelegate next)
|
||||
//{
|
||||
// if (IsTransactional)
|
||||
// {
|
||||
// ServiceLocator.in.getservice()
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using Castle.DynamicProxy;
|
||||
using Nest;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Uow.Interceptors
|
||||
{
|
||||
public class UnitOfWorkInterceptor : IInterceptor
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
public UnitOfWorkInterceptor(IUnitOfWork unitOfWork)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public void Intercept(IInvocation invocation)
|
||||
{
|
||||
if (!IsUnitOfWorkMethod(invocation.Method, out var uowAttr))
|
||||
{
|
||||
invocation.Proceed();
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_unitOfWork.BeginTran();
|
||||
//执行被拦截的方法
|
||||
invocation.Proceed();
|
||||
_unitOfWork.CommitTran();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_unitOfWork.RollbackTran();
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static bool IsUnitOfWorkMethod( MethodInfo methodInfo,out UnitOfWorkAttribute unitOfWorkAttribute)
|
||||
{
|
||||
|
||||
//Method declaration
|
||||
var attrs = methodInfo.GetCustomAttributes(true).OfType<UnitOfWorkAttribute>().ToArray();
|
||||
if (attrs.Any())
|
||||
{
|
||||
unitOfWorkAttribute = attrs.First();
|
||||
return !unitOfWorkAttribute.IsDisabled;
|
||||
}
|
||||
|
||||
if (methodInfo.DeclaringType != null)
|
||||
{
|
||||
//Class declaration
|
||||
attrs = methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true).OfType<UnitOfWorkAttribute>().ToArray();
|
||||
if (attrs.Any())
|
||||
{
|
||||
unitOfWorkAttribute = attrs.First();
|
||||
return !unitOfWorkAttribute.IsDisabled;
|
||||
}
|
||||
|
||||
////Conventional classes
|
||||
//if (typeof(IUnitOfWorkEnabled).GetTypeInfo().IsAssignableFrom(methodInfo.DeclaringType))
|
||||
//{
|
||||
// unitOfWorkAttribute = null;
|
||||
// return true;
|
||||
//}
|
||||
}
|
||||
|
||||
unitOfWorkAttribute = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user