问题
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