mirror of https://github.com/abpframework/abp.git
28 changed files with 377 additions and 134 deletions
@ -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; |
|||
} |
|||
} |
|||
} |
|||
@ -1,36 +1,19 @@ |
|||
using System; |
|||
using System.Reflection; |
|||
using Castle.DynamicProxy; |
|||
using Castle.DynamicProxy; |
|||
using Volo.Abp.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) |
|||
: base(invocation) |
|||
{ |
|||
_invocation = invocation; |
|||
|
|||
} |
|||
|
|||
public void Proceed() |
|||
{ |
|||
_invocation.Proceed(); |
|||
Invocation.Proceed(); |
|||
} |
|||
} |
|||
} |
|||
@ -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; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.DynamicProxy |
|||
{ |
|||
public interface IAbpAsyncInterceptor |
|||
{ |
|||
Task InterceptAsync(IAbpAsyncMethodInvocation invocation); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.DynamicProxy |
|||
{ |
|||
public interface IAbpAsyncMethodInvocation: IAbpMethodInvocationCore |
|||
{ |
|||
Task ProceedAsync(); |
|||
} |
|||
} |
|||
@ -1,20 +1,7 @@ |
|||
using System; |
|||
using System.Reflection; |
|||
|
|||
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(); |
|||
} |
|||
} |
|||
@ -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; } |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.TestBase.Logging |
|||
{ |
|||
public interface ICanLogOnObject |
|||
{ |
|||
List<string> Logs { get; } |
|||
} |
|||
} |
|||
@ -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> |
|||
@ -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>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.Castle.DynamicProxy; |
|||
|
|||
namespace Volo.Abp.Autofac.Interception |
|||
{ |
|||
public class Autofac_Interception_Test : CastleInterceptionTestBase<AutofacTestModule> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -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> |
|||
@ -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>(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -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"); |
|||
} |
|||
} |
|||
} |
|||
@ -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; |
|||
} |
|||
} |
|||
} |
|||
@ -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"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue