diff --git a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkHelper.cs b/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkHelper.cs new file mode 100644 index 0000000000..fe8d91faef --- /dev/null +++ b/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().ToArray(); + if (attrs.Any()) + { + return true; + } + + if (methodInfo.DeclaringType != null) + { + //Class declaration + attrs = methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true).OfType().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); + } + } +} diff --git a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs b/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs index b486d84e09..5571cd8ac0 100644 --- a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs +++ b/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()) { diff --git a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptorRegistrar.cs b/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptorRegistrar.cs index edac0be69c..90eec8c790 100644 --- a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptorRegistrar.cs +++ b/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(); } } - - 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); - } } } \ No newline at end of file