Using Simple Injector with Castle Proxy Interceptor

柔情痞子 提交于 2019-12-18 02:47:49

问题


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.


回答1:


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))
            {
                var interceptorExpression = 
                    c.GetRegistration(typeof(TInterceptor), true).BuildExpression();

                e.Expression = Expression.Convert(
                    Expression.Invoke(Expression.Constant(createProxy),
                        Expression.Constant(e.RegisteredServiceType, typeof(Type)),
                        e.Expression,
                        interceptorExpression),
                    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.



来源:https://stackoverflow.com/questions/24513530/using-simple-injector-with-castle-proxy-interceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!