【问题标题】:How do you resolve instances of IInterceptionBehavior from the container when using StructureMap DynamicProxyInterceptor?使用 StructureMap DynamicProxyInterceptor 时如何从容器中解析 IInterceptionBehavior 实例?
【发布时间】:2017-04-11 17:11:37
【问题描述】:

我正在从 Unity 迁移到 StructureMap。我已经使用了 Unity 的 InterceptionBehavior。

我认为我可以将其切换为使用 StructureMap .InterceptWith 和 DynamicProxyInterceptor,但我的拦截器具有依赖关系,我无法弄清楚如何使用 StructureMap 来组合拦截器。

var container = new Container(x =>
        {
            x.For<IMathService>().Use<MathService>()
                .InterceptWith(new DynamicProxyInterceptor<IMathService>(new IInterceptionBehavior[]
                {
                    // I WANT TO COMPOSE THESE INTERCEPTORS
                    new NegatingInterceptor(), 
                    new CachingInterceptor()
                }));
        });

目前我唯一能想到的解决方案就是从静态 IoC 类中公开我的 IContainer 并在我的拦截器中手动解决我的依赖项。

最终我可能会用装饰器替换我的动态代理,但我还没有到那个阶段。我只是想尽快重新启动并运行它,以便在开始进行其他更改之前证明其他更改都成功了。

【问题讨论】:

    标签: structuremap structuremap4 method-interception


    【解决方案1】:

    好吧,我是个白痴,你只需将一个类型数组而不是实例传递给 DynamicProxyInterceptor 构造函数

    var container = new Container(x =>
        {
            x.For<IMathService>().Use<MathService>()
                .InterceptWith(new DynamicProxyInterceptor<IMathService>(
                new Type[]
                {
                    // I WANT TO COMPOSE THESE INTERCEPTORS
                    typeof(NegatingInterceptor), 
                    typeof(CachingInterceptor)
                }));
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多