Browse Source

Check uow method in the interceptor too.

pull/96/head
Halil İbrahim Kalkan 9 years ago
parent
commit
2fa408f7b8
  1. 69
      src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkHelper.cs
  2. 12
      src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs
  3. 32
      src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptorRegistrar.cs

69
src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkHelper.cs

@ -0,0 +1,69 @@
using System.Linq;
using System.Reflection;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
namespace Volo.Abp.Uow
{
internal static class UnitOfWorkHelper
{
public static bool IsUnitOfWorkType(TypeInfo implementationType)
{
//Explicitly defined UnitOfWorkAttribute
if (HasUnitOfWorkAttribute(implementationType) || AnyMethodHasUnitOfWorkAttribute(implementationType))
{
return true;
}
//Conventional classes
if (typeof(IApplicationService).GetTypeInfo().IsAssignableFrom(implementationType) ||
typeof(IRepository).GetTypeInfo().IsAssignableFrom(implementationType))
{
return true;
}
return false;
}
public static bool IsUnitOfWorkMethod(MethodInfo methodInfo)
{
//Method declaration
var attrs = methodInfo.GetCustomAttributes(true).OfType<UnitOfWorkAttribute>().ToArray();
if (attrs.Any())
{
return true;
}
if (methodInfo.DeclaringType != null)
{
//Class declaration
attrs = methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true).OfType<UnitOfWorkAttribute>().ToArray();
if (attrs.Any())
{
return true;
}
//Conventional classes
if (typeof(IApplicationService).GetTypeInfo().IsAssignableFrom(methodInfo.DeclaringType) ||
typeof(IRepository).GetTypeInfo().IsAssignableFrom(methodInfo.DeclaringType))
{
return true;
}
}
return false;
}
private static bool AnyMethodHasUnitOfWorkAttribute(TypeInfo implementationType)
{
return implementationType
.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Any(HasUnitOfWorkAttribute);
}
private static bool HasUnitOfWorkAttribute(MemberInfo methodInfo)
{
return methodInfo.IsDefined(typeof(UnitOfWorkAttribute), true);
}
}
}

12
src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs

@ -15,7 +15,11 @@ namespace Volo.Abp.Uow
public override void Intercept(IAbpMethodInvocation invocation)
{
//TODO: Check UOW attribute and other conditions!
if (!UnitOfWorkHelper.IsUnitOfWorkMethod(invocation.Method))
{
invocation.Proceed();
return;
}
using (var uow = _unitOfWorkManager.Begin())
{
@ -26,7 +30,11 @@ namespace Volo.Abp.Uow
public override async Task InterceptAsync(IAbpMethodInvocation invocation)
{
//TODO: Check UOW attribute and other conditions!
if (!UnitOfWorkHelper.IsUnitOfWorkMethod(invocation.Method))
{
invocation.Proceed();
return;
}
using (var uow = _unitOfWorkManager.Begin())
{

32
src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptorRegistrar.cs

@ -10,40 +10,10 @@ namespace Volo.Abp.Uow
{
public static void RegisterIfNeeded(IOnServiceRegistredContext context)
{
if (IsUnitOfWorkType(context.ImplementationType.GetTypeInfo()))
if (UnitOfWorkHelper.IsUnitOfWorkType(context.ImplementationType.GetTypeInfo()))
{
context.Interceptors.Add<UnitOfWorkInterceptor>();
}
}
public static bool IsUnitOfWorkType(TypeInfo implementationType)
{
//Explicitly defined UnitOfWorkAttribute
if (HasUnitOfWorkAttribute(implementationType) || AnyMethodHasUnitOfWorkAttribute(implementationType))
{
return true;
}
//Conventional classes
if (typeof(IApplicationService).GetTypeInfo().IsAssignableFrom(implementationType) ||
typeof(IRepository).GetTypeInfo().IsAssignableFrom(implementationType))
{
return true;
}
return false;
}
private static bool AnyMethodHasUnitOfWorkAttribute(TypeInfo implementationType)
{
return implementationType
.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Any(HasUnitOfWorkAttribute);
}
private static bool HasUnitOfWorkAttribute(MemberInfo methodInfo)
{
return methodInfo.IsDefined(typeof(UnitOfWorkAttribute), true);
}
}
}
Loading…
Cancel
Save