Browse Source

Worked on async interceptors.

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
684051efa4
  1. 14
      Volo.Abp.sln
  2. 63
      src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs
  3. 44
      src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs
  4. 21
      src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpAsyncMethodInvocationAdapter.cs
  5. 26
      src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpInterceptorAdapter.cs
  6. 27
      src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapter.cs
  7. 31
      src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapterBase.cs
  8. 5
      src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionConventionalRegistrationExtensions.cs
  9. 9
      src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncInterceptor.cs
  10. 9
      src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncMethodInvocation.cs
  11. 15
      src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocation.cs
  12. 18
      src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocationCore.cs
  13. 2
      src/Volo.Abp/Volo/Abp/Threading/InternalAsyncHelper.cs
  14. 23
      src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs
  15. 2
      test/AbpTestBase/AbpTestBase.csproj
  16. 9
      test/AbpTestBase/Volo/Abp/TestBase/Logging/ICanLogOnObject.cs
  17. 6
      test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo.Abp.AspNetCore.MultiTenancy.Tests.csproj
  18. 6
      test/Volo.Abp.AspNetCore.Tests/Volo.Abp.AspNetCore.Tests.csproj
  19. 25
      test/Volo.Abp.Autofac.Tests/Volo.Abp.Autofac.Tests.csproj
  20. 16
      test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutofacTestModule.cs
  21. 9
      test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/Interception/Autofac_Interception_Test.cs
  22. 24
      test/Volo.Abp.Castle.Core.Tests/Volo.Abp.Castle.Core.Tests.csproj
  23. 30
      test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/AbpCastleCoreTestModule.cs
  24. 32
      test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/CastleInterceptionTestBase.cs
  25. 17
      test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/SimpleInterceptionTargetClass.cs
  26. 16
      test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/SimpleInterceptor.cs
  27. 6
      test/Volo.Abp.MultiTenancy.Tests/Volo.Abp.MultiTenancy.Tests.csproj
  28. 6
      test/Volo.Abp.Tests/Volo.Abp.Tests.csproj

14
Volo.Abp.sln

@ -110,6 +110,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Autofac", "src\Vol
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Castle.Core", "src\Volo.Abp.Castle.Core\Volo.Abp.Castle.Core.csproj", "{053F7446-0545-482E-9F29-9C96B926966C}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Castle.Core", "src\Volo.Abp.Castle.Core\Volo.Abp.Castle.Core.csproj", "{053F7446-0545-482E-9F29-9C96B926966C}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Autofac.Tests", "test\Volo.Abp.Autofac.Tests\Volo.Abp.Autofac.Tests.csproj", "{D8BE64D2-BD83-40F5-9783-D7FDDF668C45}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Castle.Core.Tests", "test\Volo.Abp.Castle.Core.Tests\Volo.Abp.Castle.Core.Tests.csproj", "{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -264,6 +268,14 @@ Global
{053F7446-0545-482E-9F29-9C96B926966C}.Debug|Any CPU.Build.0 = Debug|Any CPU {053F7446-0545-482E-9F29-9C96B926966C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{053F7446-0545-482E-9F29-9C96B926966C}.Release|Any CPU.ActiveCfg = Release|Any CPU {053F7446-0545-482E-9F29-9C96B926966C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{053F7446-0545-482E-9F29-9C96B926966C}.Release|Any CPU.Build.0 = Release|Any CPU {053F7446-0545-482E-9F29-9C96B926966C}.Release|Any CPU.Build.0 = Release|Any CPU
{D8BE64D2-BD83-40F5-9783-D7FDDF668C45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8BE64D2-BD83-40F5-9783-D7FDDF668C45}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8BE64D2-BD83-40F5-9783-D7FDDF668C45}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8BE64D2-BD83-40F5-9783-D7FDDF668C45}.Release|Any CPU.Build.0 = Release|Any CPU
{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -315,5 +327,7 @@ Global
{00B240B6-EC44-461A-9578-EF4F1BE9C688} = {1895A5C9-50D4-4568-9A3A-14657E615A5E} {00B240B6-EC44-461A-9578-EF4F1BE9C688} = {1895A5C9-50D4-4568-9A3A-14657E615A5E}
{CECE1288-B5A1-4A6B-BEE0-331861F94983} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} {CECE1288-B5A1-4A6B-BEE0-331861F94983} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8}
{053F7446-0545-482E-9F29-9C96B926966C} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} {053F7446-0545-482E-9F29-9C96B926966C} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8}
{D8BE64D2-BD83-40F5-9783-D7FDDF668C45} = {37087D1B-3693-4E96-983D-A69F210BDE53}
{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16} = {37087D1B-3693-4E96-983D-A69F210BDE53}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

63
src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs

@ -1,42 +1,59 @@
using System.Linq; using System;
using System.Linq;
using System.Reflection; using System.Reflection;
using Autofac.Core; using Autofac.Core;
using Autofac.Extras.DynamicProxy; using Autofac.Extras.DynamicProxy;
using Volo.Abp.Application.Services; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Castle.DynamicProxy; using Volo.Abp.Castle.DynamicProxy;
using Volo.Abp.Uow;
namespace Autofac.Builder namespace Autofac.Builder
{ {
public static class AbpRegistrationBuilderExtensions public static class AbpRegistrationBuilderExtensions
{ {
public static IRegistrationBuilder<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle> ApplyAbpConcepts<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle>( public static IRegistrationBuilder<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle> ConfigureAbpConventions<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle>(
this IRegistrationBuilder<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle> registrationBuilder) this IRegistrationBuilder<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle> registrationBuilder,
IServiceCollection services)
where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData
{ {
//TODO: Refactor to an extensible way!
var serviceType = registrationBuilder.RegistrationData.Services.OfType<IServiceWithType>().FirstOrDefault()?.ServiceType; var serviceType = registrationBuilder.RegistrationData.Services.OfType<IServiceWithType>().FirstOrDefault()?.ServiceType;
if (serviceType == null)
{
return registrationBuilder;
}
if (serviceType != null) var serviceRegistredArgs = new OnServiceRegistredArgs(serviceType);
foreach (var registrationAction in services.GetServiceRegistrationActionList())
{ {
if (typeof(IApplicationService).IsAssignableFrom(serviceType)) registrationAction.Invoke(serviceRegistredArgs);
{
if (serviceType.GetTypeInfo().IsInterface)
{
registrationBuilder = registrationBuilder.EnableInterfaceInterceptors();
}
else
{
registrationBuilder = registrationBuilder.EnableClassInterceptors();
}
registrationBuilder.InterceptedBy(
typeof(CastleAbpInterceptorAdapter<>).MakeGenericType(typeof(UnitOfWorkInterceptor))
);
}
} }
if (serviceRegistredArgs.Interceptors.Any())
{
registrationBuilder = registrationBuilder.AddInterceptors(serviceRegistredArgs.Interceptors.ToArray());
}
return registrationBuilder;
}
private static IRegistrationBuilder<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle> AddInterceptors<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle>(
this IRegistrationBuilder<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle> registrationBuilder,
Type[] interceptors
)
where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData
{
var serviceType = registrationBuilder.RegistrationData.Services.OfType<IServiceWithType>().FirstOrDefault()?.ServiceType;
registrationBuilder = serviceType.GetTypeInfo().IsInterface
? registrationBuilder.EnableInterfaceInterceptors()
: registrationBuilder.EnableClassInterceptors();
foreach (var interceptor in interceptors)
{
registrationBuilder.InterceptedBy(
typeof(CastleAbpInterceptorAdapter<>).MakeGenericType(interceptor)
);
}
return registrationBuilder; return registrationBuilder;
} }
} }

44
src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs

@ -44,17 +44,17 @@ namespace Autofac.Extensions.DependencyInjection
/// <param name="builder"> /// <param name="builder">
/// The <see cref="ContainerBuilder"/> into which the registrations should be made. /// The <see cref="ContainerBuilder"/> into which the registrations should be made.
/// </param> /// </param>
/// <param name="descriptors"> /// <param name="services">
/// The set of service descriptors to register in the container. /// The set of service descriptors to register in the container.
/// </param> /// </param>
public static void Populate( public static void Populate(
this ContainerBuilder builder, this ContainerBuilder builder,
IEnumerable<ServiceDescriptor> descriptors) IServiceCollection services)
{ {
builder.RegisterType<AutofacServiceProvider>().As<IServiceProvider>(); builder.RegisterType<AutofacServiceProvider>().As<IServiceProvider>();
builder.RegisterType<AutofacServiceScopeFactory>().As<IServiceScopeFactory>(); builder.RegisterType<AutofacServiceScopeFactory>().As<IServiceScopeFactory>();
Register(builder, descriptors); Register(builder, services);
} }
/// <summary> /// <summary>
@ -94,43 +94,43 @@ namespace Autofac.Extensions.DependencyInjection
/// <param name="builder"> /// <param name="builder">
/// The <see cref="ContainerBuilder"/> into which the registrations should be made. /// The <see cref="ContainerBuilder"/> into which the registrations should be made.
/// </param> /// </param>
/// <param name="descriptors"> /// <param name="services">
/// The set of service descriptors to register in the container. /// The set of service descriptors to register in the container.
/// </param> /// </param>
private static void Register( private static void Register(
ContainerBuilder builder, ContainerBuilder builder,
IEnumerable<ServiceDescriptor> descriptors) IServiceCollection services)
{ {
foreach (var descriptor in descriptors) foreach (var service in services)
{ {
if (descriptor.ImplementationType != null) if (service.ImplementationType != null)
{ {
// Test if the an open generic type is being registered // Test if the an open generic type is being registered
var serviceTypeInfo = descriptor.ServiceType.GetTypeInfo(); var serviceTypeInfo = service.ServiceType.GetTypeInfo();
if (serviceTypeInfo.IsGenericTypeDefinition) if (serviceTypeInfo.IsGenericTypeDefinition)
{ {
builder builder
.RegisterGeneric(descriptor.ImplementationType) .RegisterGeneric(service.ImplementationType)
.As(descriptor.ServiceType) .As(service.ServiceType)
.ConfigureLifecycle(descriptor.Lifetime); .ConfigureLifecycle(service.Lifetime);
} }
else else
{ {
builder builder
.RegisterType(descriptor.ImplementationType) .RegisterType(service.ImplementationType)
.As(descriptor.ServiceType) .As(service.ServiceType)
.ConfigureLifecycle(descriptor.Lifetime) .ConfigureLifecycle(service.Lifetime)
.ApplyAbpConcepts(); .ConfigureAbpConventions(services);
} }
} }
else if (descriptor.ImplementationFactory != null) else if (service.ImplementationFactory != null)
{ {
var registration = RegistrationBuilder.ForDelegate(descriptor.ServiceType, (context, parameters) => var registration = RegistrationBuilder.ForDelegate(service.ServiceType, (context, parameters) =>
{ {
var serviceProvider = context.Resolve<IServiceProvider>(); var serviceProvider = context.Resolve<IServiceProvider>();
return descriptor.ImplementationFactory(serviceProvider); return service.ImplementationFactory(serviceProvider);
}) })
.ConfigureLifecycle(descriptor.Lifetime) .ConfigureLifecycle(service.Lifetime)
.CreateRegistration(); .CreateRegistration();
builder.RegisterComponent(registration); builder.RegisterComponent(registration);
@ -138,9 +138,9 @@ namespace Autofac.Extensions.DependencyInjection
else else
{ {
builder builder
.RegisterInstance(descriptor.ImplementationInstance) .RegisterInstance(service.ImplementationInstance)
.As(descriptor.ServiceType) .As(service.ServiceType)
.ConfigureLifecycle(descriptor.Lifetime); .ConfigureLifecycle(service.Lifetime);
} }
} }
} }

21
src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpAsyncMethodInvocationAdapter.cs

@ -0,0 +1,21 @@
using System.Threading.Tasks;
using Castle.DynamicProxy;
using Volo.Abp.DynamicProxy;
namespace Volo.Abp.Castle.DynamicProxy
{
public class CastleAbpAsyncMethodInvocationAdapter : CastleAbpMethodInvocationAdapterBase, IAbpAsyncMethodInvocation
{
public CastleAbpAsyncMethodInvocationAdapter(IInvocation invocation)
: base(invocation)
{
}
public Task ProceedAsync()
{
Invocation.Proceed();
return (Task)Invocation.ReturnValue;
}
}
}

26
src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpInterceptorAdapter.cs

@ -1,23 +1,10 @@
using Castle.DynamicProxy; using Castle.DynamicProxy;
using Volo.Abp.DynamicProxy; using Volo.Abp.DynamicProxy;
using Volo.Abp.Threading;
using Volo.ExtensionMethods;
namespace Volo.Abp.Castle.DynamicProxy namespace Volo.Abp.Castle.DynamicProxy
{ {
public class CastleAbpInterceptorAdapter : IInterceptor
{
private readonly IAbpInterceptor _abpInterceptor;
public CastleAbpInterceptorAdapter(IAbpInterceptor abpInterceptor)
{
_abpInterceptor = abpInterceptor;
}
public void Intercept(IInvocation invocation)
{
_abpInterceptor.Intercept(new CastleAbpMethodInvocationAdapter(invocation));
}
}
public class CastleAbpInterceptorAdapter<TInterceptor> : IInterceptor public class CastleAbpInterceptorAdapter<TInterceptor> : IInterceptor
where TInterceptor : IAbpInterceptor where TInterceptor : IAbpInterceptor
{ {
@ -30,7 +17,14 @@ namespace Volo.Abp.Castle.DynamicProxy
public void Intercept(IInvocation invocation) public void Intercept(IInvocation invocation)
{ {
_abpInterceptor.Intercept(new CastleAbpMethodInvocationAdapter(invocation)); if (invocation.MethodInvocationTarget.IsAsync() && _abpInterceptor is IAbpAsyncInterceptor)
{
_abpInterceptor.As<IAbpAsyncInterceptor>().InterceptAsync(new CastleAbpAsyncMethodInvocationAdapter(invocation));
}
else
{
_abpInterceptor.Intercept(new CastleAbpMethodInvocationAdapter(invocation));
}
} }
} }
} }

27
src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapter.cs

@ -1,36 +1,19 @@
using System; using Castle.DynamicProxy;
using System.Reflection;
using Castle.DynamicProxy;
using Volo.Abp.DynamicProxy; using Volo.Abp.DynamicProxy;
namespace Volo.Abp.Castle.DynamicProxy namespace Volo.Abp.Castle.DynamicProxy
{ {
public class CastleAbpMethodInvocationAdapter : IAbpMethodInvocation public class CastleAbpMethodInvocationAdapter : CastleAbpMethodInvocationAdapterBase, IAbpMethodInvocation
{ {
public object[] Arguments => _invocation.Arguments;
public Type[] GenericArguments => _invocation.GenericArguments;
public object TargetObject => _invocation.InvocationTarget;
public MethodInfo Method => _invocation.MethodInvocationTarget;
public object ReturnValue
{
get => _invocation.ReturnValue;
set => _invocation.ReturnValue = value;
}
private readonly IInvocation _invocation;
public CastleAbpMethodInvocationAdapter(IInvocation invocation) public CastleAbpMethodInvocationAdapter(IInvocation invocation)
: base(invocation)
{ {
_invocation = invocation;
} }
public void Proceed() public void Proceed()
{ {
_invocation.Proceed(); Invocation.Proceed();
} }
} }
} }

31
src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapterBase.cs

@ -0,0 +1,31 @@
using System;
using System.Reflection;
using Castle.DynamicProxy;
using Volo.Abp.DynamicProxy;
namespace Volo.Abp.Castle.DynamicProxy
{
public abstract class CastleAbpMethodInvocationAdapterBase : IAbpMethodInvocationCore
{
public object[] Arguments => Invocation.Arguments;
public Type[] GenericArguments => Invocation.GenericArguments;
public object TargetObject => Invocation.InvocationTarget;
public MethodInfo Method => Invocation.MethodInvocationTarget;
public object ReturnValue
{
get => Invocation.ReturnValue;
set => Invocation.ReturnValue = value;
}
protected IInvocation Invocation { get; }
protected CastleAbpMethodInvocationAdapterBase(IInvocation invocation)
{
Invocation = invocation;
}
}
}

5
src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionConventionalRegistrationExtensions.cs

@ -55,6 +55,11 @@ namespace Microsoft.Extensions.DependencyInjection
return services; return services;
} }
public static IServiceCollection AddType<TType>(this IServiceCollection services)
{
return services.AddType(typeof(TType));
}
public static IServiceCollection AddType(this IServiceCollection services, Type type) public static IServiceCollection AddType(this IServiceCollection services, Type type)
{ {
foreach (var registrar in services.GetConventionalRegistrars()) foreach (var registrar in services.GetConventionalRegistrars())

9
src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncInterceptor.cs

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Volo.Abp.DynamicProxy
{
public interface IAbpAsyncInterceptor
{
Task InterceptAsync(IAbpAsyncMethodInvocation invocation);
}
}

9
src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncMethodInvocation.cs

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Volo.Abp.DynamicProxy
{
public interface IAbpAsyncMethodInvocation: IAbpMethodInvocationCore
{
Task ProceedAsync();
}
}

15
src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocation.cs

@ -1,20 +1,7 @@
using System;
using System.Reflection;
namespace Volo.Abp.DynamicProxy namespace Volo.Abp.DynamicProxy
{ {
public interface IAbpMethodInvocation public interface IAbpMethodInvocation: IAbpMethodInvocationCore
{ {
object[] Arguments { get; }
Type[] GenericArguments { get; }
object TargetObject { get; }
MethodInfo Method { get; }
object ReturnValue { get; set; }
void Proceed(); void Proceed();
} }
} }

18
src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocationCore.cs

@ -0,0 +1,18 @@
using System;
using System.Reflection;
namespace Volo.Abp.DynamicProxy
{
public interface IAbpMethodInvocationCore
{
object[] Arguments { get; }
Type[] GenericArguments { get; }
object TargetObject { get; }
MethodInfo Method { get; }
object ReturnValue { get; set; }
}
}

2
src/Volo.Abp/Volo/Abp/Threading/InternalAsyncHelper.cs

@ -4,7 +4,7 @@ using System.Threading.Tasks;
namespace Volo.Abp.Threading namespace Volo.Abp.Threading
{ {
internal static class InternalAsyncHelper public static class InternalAsyncHelper //TODO: Rename since it's not internal anymore!
{ {
public static async Task AwaitTaskWithFinally(Task actualReturnValue, Action<Exception> finalAction) public static async Task AwaitTaskWithFinally(Task actualReturnValue, Action<Exception> finalAction)
{ {

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

@ -5,7 +5,7 @@ using Volo.DependencyInjection;
namespace Volo.Abp.Uow namespace Volo.Abp.Uow
{ {
public class UnitOfWorkInterceptor : IAbpInterceptor, ITransientDependency public class UnitOfWorkInterceptor : IAbpInterceptor, IAbpAsyncInterceptor, ITransientDependency
{ {
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
@ -13,31 +13,30 @@ namespace Volo.Abp.Uow
{ {
_unitOfWorkManager = unitOfWorkManager; _unitOfWorkManager = unitOfWorkManager;
} }
public void Intercept(IAbpMethodInvocation invocation) public void Intercept(IAbpMethodInvocation invocation)
{ {
//TODO: Check UOW attribute and other conditions! //TODO: Check UOW attribute and other conditions!
if (invocation.Method.IsAsync()) using (var uow = _unitOfWorkManager.Begin())
{
PerformAsyncUow(invocation);
}
else
{ {
PerformSyncUow(invocation); invocation.Proceed();
uow.Complete();
} }
} }
private void PerformSyncUow(IAbpMethodInvocation invocation) public async Task InterceptAsync(IAbpAsyncMethodInvocation invocation)
{ {
//TODO: Check UOW attribute and other conditions!
using (var uow = _unitOfWorkManager.Begin()) using (var uow = _unitOfWorkManager.Begin())
{ {
invocation.Proceed(); await invocation.ProceedAsync();
uow.Complete(); await uow.CompleteAsync();
} }
} }
private void PerformAsyncUow(IAbpMethodInvocation invocation) private void PerformAsyncUow(IAbpMethodInvocation invocation)
{ {
var uow = _unitOfWorkManager.Begin(); var uow = _unitOfWorkManager.Begin();

2
test/AbpTestBase/AbpTestBase.csproj

@ -16,10 +16,12 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="NSubstitute" Version="2.0.2" /> <PackageReference Include="NSubstitute" Version="2.0.2" />
<PackageReference Include="Shouldly" Version="2.8.2" /> <PackageReference Include="Shouldly" Version="2.8.2" />
<PackageReference Include="xunit" Version="2.2.0" /> <PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.extensibility.execution" Version="2.2.0" /> <PackageReference Include="xunit.extensibility.execution" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

9
test/AbpTestBase/Volo/Abp/TestBase/Logging/ICanLogOnObject.cs

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Volo.Abp.TestBase.Logging
{
public interface ICanLogOnObject
{
List<string> Logs { get; }
}
}

6
test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo.Abp.AspNetCore.MultiTenancy.Tests.csproj

@ -18,12 +18,6 @@
<ProjectReference Include="..\..\src\Volo.Abp.AspNetCore.TestBase\Volo.Abp.AspNetCore.TestBase.csproj" /> <ProjectReference Include="..\..\src\Volo.Abp.AspNetCore.TestBase\Volo.Abp.AspNetCore.TestBase.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup> </ItemGroup>

6
test/Volo.Abp.AspNetCore.Tests/Volo.Abp.AspNetCore.Tests.csproj

@ -17,10 +17,4 @@
<ProjectReference Include="..\..\src\Volo.Abp.AspNetCore\Volo.Abp.AspNetCore.csproj" /> <ProjectReference Include="..\..\src\Volo.Abp.AspNetCore\Volo.Abp.AspNetCore.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
</Project> </Project>

25
test/Volo.Abp.Autofac.Tests/Volo.Abp.Autofac.Tests.csproj

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<AssemblyName>Volo.Abp.Autofac.Tests</AssemblyName>
<PackageId>Volo.Abp.Autofac.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" />
<ProjectReference Include="..\Volo.Abp.Castle.Core.Tests\Volo.Abp.Castle.Core.Tests.csproj" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>

16
test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutofacTestModule.cs

@ -0,0 +1,16 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Castle;
using Volo.Abp.Castle.DynamicProxy;
using Volo.Abp.Modularity;
namespace Volo.Abp.Autofac
{
[DependsOn(typeof(AbpAutofacModule), typeof(AbpCastleCoreTestModule))]
public class AutofacTestModule : AbpModule
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddAssemblyOf<AbpCastleCoreTestModule>();
}
}
}

9
test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/Interception/Autofac_Interception_Test.cs

@ -0,0 +1,9 @@
using Volo.Abp.Castle.DynamicProxy;
namespace Volo.Abp.Autofac.Interception
{
public class Autofac_Interception_Test : CastleInterceptionTestBase<AutofacTestModule>
{
}
}

24
test/Volo.Abp.Castle.Core.Tests/Volo.Abp.Castle.Core.Tests.csproj

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<AssemblyName>Volo.Abp.Castle.Core.Tests</AssemblyName>
<PackageId>Volo.Abp.Castle.Core.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Volo.Abp.Castle.Core\Volo.Abp.Castle.Core.csproj" />
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>

30
test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/AbpCastleCoreTestModule.cs

@ -0,0 +1,30 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Castle.DynamicProxy;
using Volo.Abp.Modularity;
using Volo.Abp.Uow;
namespace Volo.Abp.Castle
{
[DependsOn(typeof(AbpCastleCoreModule))]
public class AbpCastleCoreTestModule : AbpModule
{
public override void PreConfigureServices(IServiceCollection services)
{
services.OnServiceRegistred(RegisterTestInterceptors);
}
public override void ConfigureServices(IServiceCollection services)
{
services.AddAssemblyOf<AbpCastleCoreTestModule>();
}
private static void RegisterTestInterceptors(IOnServiceRegistredArgs registration)
{
//TODO: Create an attribute to add interceptors!
if (typeof(SimpleInterceptionTargetClass) == registration.ImplementationType)
{
registration.Interceptors.Add<SimpleInterceptor>();
}
}
}
}

32
test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/CastleInterceptionTestBase.cs

@ -0,0 +1,32 @@
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Modularity;
using Volo.Abp.TestBase;
using Xunit;
namespace Volo.Abp.Castle.DynamicProxy
{
public abstract class CastleInterceptionTestBase<TStartupModule> : AbpIntegratedTest<TStartupModule>
where TStartupModule : IAbpModule
{
[Fact]
public void Should_Intercept_Sync_Methods()
{
//Arrange
var target = ServiceProvider.GetService<SimpleInterceptionTargetClass>();
//Act
var result = target.GetValue();
//Assert
result.ShouldBe(42);
target.Logs.Count.ShouldBe(3);
target.Logs[0].ShouldBe("BeforeInvocation");
target.Logs[1].ShouldBe("ExecutingGetValue");
target.Logs[2].ShouldBe("AfterInvocation");
}
}
}

17
test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/SimpleInterceptionTargetClass.cs

@ -0,0 +1,17 @@
using System.Collections.Generic;
using Volo.Abp.TestBase.Logging;
using Volo.DependencyInjection;
namespace Volo.Abp.Castle.DynamicProxy
{
public class SimpleInterceptionTargetClass : ITransientDependency, ICanLogOnObject
{
public List<string> Logs { get; } = new List<string>();
public virtual int GetValue()
{
Logs.Add("ExecutingGetValue");
return 42;
}
}
}

16
test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/SimpleInterceptor.cs

@ -0,0 +1,16 @@
using Volo.Abp.DynamicProxy;
using Volo.Abp.TestBase.Logging;
using Volo.DependencyInjection;
namespace Volo.Abp.Castle.DynamicProxy
{
public class SimpleInterceptor : IAbpInterceptor, ITransientDependency
{
public void Intercept(IAbpMethodInvocation invocation)
{
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("BeforeInvocation");
invocation.Proceed();
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("AfterInvocation");
}
}
}

6
test/Volo.Abp.MultiTenancy.Tests/Volo.Abp.MultiTenancy.Tests.csproj

@ -17,12 +17,6 @@
<ProjectReference Include="..\..\src\Volo.Abp.MultiTenancy\Volo.Abp.MultiTenancy.csproj" /> <ProjectReference Include="..\..\src\Volo.Abp.MultiTenancy\Volo.Abp.MultiTenancy.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup> </ItemGroup>

6
test/Volo.Abp.Tests/Volo.Abp.Tests.csproj

@ -21,12 +21,6 @@
<ProjectReference Include="..\..\src\Volo.Abp\Volo.Abp.csproj" /> <ProjectReference Include="..\..\src\Volo.Abp\Volo.Abp.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup> </ItemGroup>

Loading…
Cancel
Save