使用StructureMap DynamicProxyInterceptor时,如何从容器解析IInterceptionBehavior实例? [英] How do you resolve instances of IInterceptionBehavior from the container when using StructureMap DynamicProxyInterceptor?
问题描述
我正在从Unity迁移到StructureMap.我已经利用了Unity的InterceptionBehavior.
I'm migrating from Unity to StructureMap. I've made some use of Unity's InterceptionBehavior.
我以为我可以将其切换为使用StructureMap .InterceptWith
和DynamicProxyInterceptor
,但是我的拦截器具有依赖性,因此我无法弄清楚如何使用StructureMap构成拦截器.
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()
}));
});
目前,我唯一能想到的解决方案是从静态IoC类公开我的IContainer,并在拦截器中手动解析我的依赖项.
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.
推荐答案
好吧,我是个白痴,您只是将类型数组而不是实例传递给DynamicProxyInterceptor构造函数
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)
}));
});
这篇关于使用StructureMap DynamicProxyInterceptor时,如何从容器解析IInterceptionBehavior实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!