Skip to content

Commit

Permalink
Add service middleware and associated tests for DynamicProxy
Browse files Browse the repository at this point in the history
DelegateMiddleware class is copied from autofac, after autofac add the overload method, remove this. Additionally, ServiceMiddlewareRegistrationExtensions has been added to provide static methods for registering middleware services. Tests have been provided to ensure correct interception of public interfaces. The Moq testing library has been added as a package reference.
  • Loading branch information
Fei Xu committed Dec 18, 2024
1 parent f731a36 commit b183fbd
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="6.5.0" />
<PackageReference Include="Autofac" Version="8.2.0" />
<PackageReference Include="Castle.Core" Version="5.1.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" Condition="Exists('$(MSBuildThisFileDirectory)../../.git')">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Autofac.Core.Resolving.Pipeline;
using Castle.DynamicProxy;

namespace Autofac.Extras.DynamicProxy;

/// <summary>
/// Provides a set of static methods for registering middleware services.
/// </summary>
public static class ServiceMiddlewareRegistrationExtensions
{
/// <summary>
/// Represents an extension method on the <see cref="ContainerBuilder"/> for enabling interface interceptors.
/// </summary>
/// <typeparam name="TService">The type of the service to enable interceptors for.</typeparam>
/// <param name="builder">The container builder.</param>
/// <param name="options">The proxy generation options.</param>
public static void EnableInterfaceInterceptors<TService>(this ContainerBuilder builder, ProxyGenerationOptions? options = null)
{
builder.RegisterServiceMiddleware<TService>(PipelinePhase.ScopeSelection, (context, next) =>
{
next(context);
ProxyHelpers.ApplyProxy(context, options);
});
}

/// <summary>
/// Represents an extension method on the <see cref="ContainerBuilder"/> for enabling interface interceptors.
/// </summary>
/// <param name="builder">The container builder.</param>
/// <param name="serviceType">The type of the service to enable interceptors for.</param>
/// <param name="options">The proxy generation options.</param>
public static void EnableInterfaceInterceptors(this ContainerBuilder builder, Type serviceType, ProxyGenerationOptions? options = null)
{
builder.RegisterServiceMiddleware(serviceType, PipelinePhase.ScopeSelection, (context, next) =>
{
next(context);
ProxyHelpers.ApplyProxy(context, options);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Castle.DynamicProxy;
using Moq;
using IInvocation = Castle.DynamicProxy.IInvocation;

namespace Autofac.Extras.DynamicProxy.Test;

public class ServiceMiddlewareInterfaceInterceptorsFixture
{
public interface IPublicInterface
{
string PublicMethod();
}

public interface IDecoratorInterface
{
void Decorate();
}

[Fact]
public void InterceptsPublicInterfacesUseGenericMethod()
{
Mock<IDecoratorInterface> mockDecorator = new();
ContainerBuilder builder = new();
builder.RegisterType<StringMethodInterceptor>();
builder.RegisterDecorator<Decorator, IPublicInterface>();
builder.RegisterInstance(mockDecorator.Object);
builder
.RegisterType<Interceptable>()
.InterceptedBy(typeof(StringMethodInterceptor))
.As<IPublicInterface>();
builder.EnableInterfaceInterceptors<IPublicInterface>();
IContainer container = builder.Build();
IPublicInterface obj = container.Resolve<IPublicInterface>();
Assert.Equal("intercepted-PublicMethod", obj.PublicMethod());
mockDecorator.Verify(e => e.Decorate(), Times.Never);
}

[Fact]
public void InterceptsPublicInterfacesUseNoneGenericMethod()
{
Mock<IDecoratorInterface> mockDecorator = new();
ContainerBuilder builder = new();
builder.RegisterType<StringMethodInterceptor>();
builder.RegisterDecorator<Decorator, IPublicInterface>();
builder.RegisterInstance(mockDecorator.Object);
builder
.RegisterType<Interceptable>()
.InterceptedBy(typeof(StringMethodInterceptor))
.As<IPublicInterface>();
builder.EnableInterfaceInterceptors(typeof(IPublicInterface));
IContainer container = builder.Build();
IPublicInterface obj = container.Resolve<IPublicInterface>();
Assert.Equal("intercepted-PublicMethod", obj.PublicMethod());
mockDecorator.Verify(e => e.Decorate(), Times.Never);
}

public class Decorator : IPublicInterface
{
private readonly IPublicInterface _decoratedService;
private readonly IDecoratorInterface _decoratorInterface;

public Decorator(IPublicInterface decoratedService, IDecoratorInterface decoratorInterface)
{
_decoratedService = decoratedService;
_decoratorInterface = decoratorInterface;
}

public string PublicMethod()
{
_decoratorInterface.Decorate();
return _decoratedService.PublicMethod();
}
}

public class Interceptable : IPublicInterface
{
public string PublicMethod() => throw new NotImplementedException();
}

private class StringMethodInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
if (invocation.Method.ReturnType == typeof(string))
{
invocation.ReturnValue = "intercepted-" + invocation.Method.Name;
}
else
{
invocation.Proceed();
}
}
}
}

0 comments on commit b183fbd

Please sign in to comment.