Added UnitOfWorkInterceptorRegistrar.

pull/96/head
Halil İbrahim Kalkan 8 years ago
parent bec0f6bec2
commit 655da45b34

@ -1,23 +1,9 @@
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Application.Services;
using System.Threading.Tasks;
using Volo.Abp.DynamicProxy;
using Volo.DependencyInjection;
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
{
private readonly IUnitOfWorkManager _unitOfWorkManager;

@ -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