-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add service middleware and associated tests for DynamicProxy
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
Showing
4 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Autofac.Extras.DynamicProxy/ServiceMiddlewareRegistrationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
test/Autofac.Extras.DynamicProxy.Test/ServiceMiddlewareInterfaceInterceptorsFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |