|
DynamicProxy2
DynamicProxy2 can be used with Autofac for AOP functionality
IntroductionDynamicProxy2, from the Castle Project, provides a method interception framework. The AutofactContrib DynamicProxy2 integration enables method calls on Autofac components to be intercepted by other components. Common use-cases are transaction handling, logging, and declarative security. Required References
UsageAdd the interception module itself to Autofac: builder.RegisterModule(new StandardInterceptionModule()); InterceptorsInterceptors implement the DynamicProxy IInterceptor interface: public class CallLogger : IInterceptor
{
TextWriter _output;
public CallLogger(TextWriter output)
{
_output = output;
}
public void Intercept(IInvocation invocation)
{
_output.Write("Calling method {0} with parameters {1}... ",
invocation.Method.Name,
string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray()));
invocation.Proceed();
_output.WriteLine("Done: result was {0}.", invocation.ReturnValue);
}
}Interceptors must be registered with the container: builder.Register(c => new CallLogger(Console.Out)).Named("log-calls");Interceptors can be registered as a particular typed service or with a name as required. Attaching to ComponentsTo attach a service to an interceptor, either attribute its implementation type with the InterceptAttribute: [Intercept("log-calls")]
public class MyCallsAreLogged
{
public virtual int Method();
}Or use the InterceptedBy() extension: builder.Register<MyCallsAreLogged>().InterceptedBy("log-calls");Or add an IEnumerable<Service> to its extended properties with the ExtendedPropertyInterceptorProvider.InterceptorsPropertyName key: var interceptors = new Service[] { new NamedService("log-calls") };
builder.Register<MyCallsAreLogged>()
.WithExtendedProperty(ExtendedPropertyInterceptorProvider.InterceptorsPropertyName, interceptors);Usage with ExpressionsComponents created using expressions, or those registered as instances, cannot be subclassed by the DynamicProxy2 engine. In these cases, it is necessary to use interface-based proxies. Flexible InteceptionThe simplest way to enable this behaviour is to use the FlexibleInterceptionModule instead of the standard one: builder.RegisterModule(new FlexibleInterceptionModule()); For tighter control of how proxies are attached, see the InterceptionModule base class. Component RegistrationsTo enable proxying via interfaces, the component must provide its services through interfaces only. For best performance, all such service interfaces should be part of the registration, i.e. included in As<X>() clauses. |
Sign in to add a comment
I'm thinking whether it would make sense to reuse some of the stuff from the EntLib? PIAB (http://blogs.msdn.com/edjez/archive/2007/02/23/policy-injection-app-block-behind-the-scenes.aspx) such as matching rules, the handlers pipeline, etc... and make for a full AOP-like extension maybe based on cecil rather than dp2? (there are known CLR issues around codegen of generic types w/constraints using reflection.emit)