How do you resolve instances of IInterceptionBehavior from the container when using StructureMap DynamicProxyInterceptor?

和自甴很熟 提交于 2019-12-08 03:30:11

问题


I'm migrating from Unity to StructureMap. I've made some use of Unity's InterceptionBehavior.

I thought I could switch that to use StructureMap .InterceptWith and the DynamicProxyInterceptor but my interceptors have dependencies and I can't work out how to compose the interceptors using 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()
                }));
        });

At the moment the only thing I can think that might be a solution is to expose my IContainer from the static IoC class and resolve my dependencies manually in my interceptor.

Eventually I'll probably get around to replacing my dynamic proxies with decorators but I'm not quite at that stage yet. I just want to get it up and running again as soon as possible so I can prove the other changes are all successful before I start to make additional changes.


回答1:


Okay, I'm an idiot, you just pass an array of types instead of instances to the DynamicProxyInterceptor constructor

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)
            }));
    });


来源:https://stackoverflow.com/questions/43352278/how-do-you-resolve-instances-of-iinterceptionbehavior-from-the-container-when-us

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