Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Simple Injector with Castle Proxy Interceptor

I'm using Simple Injector in my asp.net mvc 4 project.

I can't figure out how can I use Simple Injector with castle proxy interceptor.

like image 219
iknowitwasyoufredo Avatar asked Jul 01 '14 15:07

iknowitwasyoufredo


1 Answers

There's in fact a section about Interception in the Simple Injector documentation that pretty clearly describes how to do interception. The code samples given there don't show how to work with Castle DynamicProxy, but you actually need to change a few lines of code to get this working.

If you use the Interception Extensions code snippet, to get it working, you only need to remove the IInterceptor and IInvocation interfaces, add a using Castle.DynamicProxy on the top of the file, and replace the generic Interceptor with the following:

public static class Interceptor
{
    private static readonly ProxyGenerator generator = new ProxyGenerator();

    public static object CreateProxy(Type type, IInterceptor interceptor,
        object target)
    {
        return generator.CreateInterfaceProxyWithTarget(type, target, interceptor);
    }
}

But at a minimum, this would be the code you need to get interception working with Castle DynamicProxy:

using System;
using System.Linq.Expressions;
using Castle.DynamicProxy;
using SimpleInjector;

public static class InterceptorExtensions
{
    private static readonly ProxyGenerator generator = new ProxyGenerator();

    private static readonly Func<Type, object, IInterceptor, object> createProxy =
        (p, t, i) => generator.CreateInterfaceProxyWithTarget(p, t, i);

    public static void InterceptWith<TInterceptor>(this Container c, 
        Predicate<Type> predicate)
        where TInterceptor : class, IInterceptor
    {
        c.ExpressionBuilt += (s, e) =>
        {
            if (predicate(e.RegisteredServiceType))
            {
                e.Expression = Expression.Convert(
                    Expression.Invoke(
                        Expression.Constant(createProxy),
                        Expression.Constant(e.RegisteredServiceType, typeof(Type)),
                        e.Expression,
                        c.GetRegistration(typeof(TInterceptor), true).BuildExpression()),
                    e.RegisteredServiceType);
            }
        };
    }
}

This is how to use this:

container.InterceptWith<MonitoringInterceptor>(
    type => type.IsInterface && type.Name.EndsWith("Repository"));

This allows intercepting all interface registrations which name end with 'Repository' to be intercepted with a transient MonitoringInterceptor.

like image 183
Steven Avatar answered Sep 22 '22 07:09

Steven