使用简单的喷油器与城堡代理拦截 [英] Using Simple Injector with Castle Proxy Interceptor

查看:313
本文介绍了使用简单的喷油器与城堡代理拦截的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的asp.net的MVC 4项目简单的注射器。



我想不出我如何使用与城堡代理拦截简单的注射器。


解决方案

有其实关于拦截在节简单注射器文档那个漂亮的清楚地描述如何去做拦截。给那里的代码示例不显示如何使用城堡DynamicProxy工作,但你确实需要改变几行代码得到这个工作。



如果您使用拦截扩展的代码片段,得到它的工作,你只需要删除 IInterceptor IInvocation 界面中,添加使用Castle.DynamicProxy 在文件的顶部,并用以下内容替换普通的拦截



<预类=郎-CS prettyprint -override> 公共静态类拦截
{
私人静态只读ProxyGenerator发生器=新ProxyGenerator();

公共静态对象CreateProxy(类型类型,IInterceptor拦截,
对象目标)
{
返回generator.CreateInterfaceProxyWithTarget(类型,目标,拦截器);
}
}



但至少,这将是你的代码需要获得与拦截城堡DynamicProxy工作:



<预类=郎-CS prettyprint-覆盖> 使用系统;使用System.Linq.Expressions
;
使用Castle.DynamicProxy;
使用SimpleInjector;

公共静态类InterceptorExtensions
{
私人静态只读ProxyGenerator发生器=新ProxyGenerator();

私人静态只读Func键<类型,对象,IInterceptor,对象> createProxy =
(P,T,I)=> generator.CreateInterfaceProxyWithTarget(P,T,I);

公共静态无效InterceptWith< TInterceptor>(这个容器C,
谓<类型>谓语)
式TInterceptor:类,IInterceptor
{
C .ExpressionBuilt + =(S,E)=>
{
如果(谓词(e.RegisteredServiceType))
{
变种interceptorExpression =
c.GetRegistration(typeof运算(TInterceptor),真).BuildExpression();

e.Expression = Expression.Convert(
Expression.Invoke(Expression.Constant(createProxy),
Expression.Constant(e.RegisteredServiceType的typeof(类型)),
e.Expression,
interceptorExpression),
e.RegisteredServiceType);
}
};
}
}

这是如何使用这样的:



<预类=郎-CS prettyprint-覆盖> container.InterceptWith< MonitoringInterceptor>(
型=> type.IsInterface和放大器;&放大器;类型.Name.EndsWith(库));

这允许拦截所有界面注册该名称以'仓库'端有一个短暂的<$被截获C $ C> MonitoringInterceptor 。


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.

解决方案

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.

这篇关于使用简单的喷油器与城堡代理拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆