mirror of https://github.com/abpframework/abp.git
2 changed files with 50 additions and 15 deletions
@ -0,0 +1,49 @@ |
|||||
|
using System.Linq; |
||||
|
using System.Reflection; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Application.Services; |
||||
|
using Volo.Abp.Domain.Repositories; |
||||
|
|
||||
|
namespace Volo.Abp.Uow |
||||
|
{ |
||||
|
public static class UnitOfWorkInterceptorRegistrar |
||||
|
{ |
||||
|
public static void RegisterIfNeeded(IOnServiceRegistredContext context) |
||||
|
{ |
||||
|
if (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…
Reference in new issue