Browse Source

Added UnitOfWorkInterceptorRegistrar.

pull/96/head
Halil İbrahim Kalkan 9 years ago
parent
commit
655da45b34
  1. 16
      src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs
  2. 49
      src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptorRegistrar.cs

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

@ -1,23 +1,9 @@
using System.Reflection; using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Application.Services;
using Volo.Abp.DynamicProxy; using Volo.Abp.DynamicProxy;
using Volo.DependencyInjection; using Volo.DependencyInjection;
namespace Volo.Abp.Uow namespace Volo.Abp.Uow
{ {
public static class UnitOfWorkInterceptorRegistrar
{
public static void RegisterIfNeeded(IOnServiceRegistredContext context)
{
if (typeof(IApplicationService).GetTypeInfo().IsAssignableFrom(context.ImplementationType))
{
context.Interceptors.Add<UnitOfWorkInterceptor>();
}
}
}
public class UnitOfWorkInterceptor : AbpInterceptor, ITransientDependency public class UnitOfWorkInterceptor : AbpInterceptor, ITransientDependency
{ {
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;

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

@ -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…
Cancel
Save