截取使用StructureMap 3. * [英] Interception Using StructureMap 3.*

查看:172
本文介绍了截取使用StructureMap 3. *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Castle.DynamicProxy和StructureMap 2.6 API进行拦截,但现在不能使用StructureMap 3.0。有人可以帮我找到更新的文档甚至演示?我发现的一切似乎都是旧版本。例如StructureMap.Interceptors.TypeInterceptor接口等。

I've done interception using Castle.DynamicProxy and StructureMap 2.6 API but now can't do it using StructureMap 3.0. Could anyone help me find updated documentation or even demo? Everything that I've found seems to be about old versions. e.g. StructureMap.Interceptors.TypeInterceptor interface etc.

推荐答案

HAHAA!我f ***在做到了!以下是如何:

HAHAA! I f***in did it! Here's how:

public class ServiceSingletonConvention : DefaultConventionScanner
{
    public override void Process(Type type, Registry registry)
    {
        base.Process(type, registry);

        if (type.IsInterface || !type.Name.ToLower().EndsWith("service")) return;

        var pluginType = FindPluginType(type);

        var delegateType = typeof(Func<,>).MakeGenericType(pluginType, pluginType);

        // Create FuncInterceptor class with generic argument +
        var d1 = typeof(FuncInterceptor<>);

        Type[] typeArgs = { pluginType };

        var interceptorType = d1.MakeGenericType(typeArgs);
        // -

        // Create lambda expression for passing it to the FuncInterceptor constructor +
        var arg = Expression.Parameter(pluginType, "x");

        var method = GetType().GetMethod("GetProxy").MakeGenericMethod(pluginType);

        // Crate method calling expression
        var methodCall = Expression.Call(method, arg);

        // Create the lambda expression
        var lambda = Expression.Lambda(delegateType, methodCall, arg);
        // -

        // Create instance of the FuncInterceptor
        var interceptor = Activator.CreateInstance(interceptorType, lambda, "");

        registry.For(pluginType).Singleton().Use(type).InterceptWith(interceptor as IInterceptor);
    }

    public static T GetProxy<T>(object service)
    {
        var proxyGeneration = new ProxyGenerator();

        var result = proxyGeneration.CreateInterfaceProxyWithTarget(
           typeof(T),
           service,
           (Castle.DynamicProxy.IInterceptor)(new MyInterceptor())
           );

        return (T)result;
    }
}

这里的问题是SM 3. *允许拦截对于已知类型,即执行如下操作:

The problem here was that SM 3.* allows interception for known types, i.e. doing something like this:

expression.For<IService>().Use<Service>().InterceptWith(new FuncInterceptor<IService>(service => GetProxyFrom(service)));

但是,如果您希望在自定义扫描约定中包含拦截逻辑,拦截具有特定签名的所有类型的实例(在我的情况下,名称以'service'结尾的类型)?

But what if you'd like to include the interception logic inside your custom scanning convention where you want to intercept all instances of type with specific signature (types having name ending on 'service', in my case)?

这是我使用Expression API和反射完成的。

That's what I've accomplished using Expression API and reflection.

另外,我在这里使用Castle.DinamicProxy为我的服务创建代理对象。

Also, I'm using here Castle.DinamicProxy for creating proxy objects for my services.

希望有人否则会发现这有帮助:)

Hope someone else will find this helpful :)

这篇关于截取使用StructureMap 3. *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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