我试图了解Ninject.Extensions.Interception
3.0.0.8 如何为我的课程构建动态代理。我发现,当我使用继承自的属性装饰具体类时,InterceptAttribute
或者当我在绑定时使用方法直接拦截时,Intercept()
Ninject 返回装饰类的动态代理,而不是普通类型。
我有一个IPolicySearchPresenter
接口,我将其绑定到FlexPolicySearchPresenter
添加异常记录器拦截器的具体类型:
Bind<IExceptionInterceptor>().To<ExceptionInterceptor>();
Bind<IPolicySearchPresenter>().To<FlexPolicySearchPresenter>().Intercept().With<IExceptionInterceptor>();
问题是当我检查该绑定的返回类型时:
var proxy = Kernel.Get<IPolicySearchPresenter>();
我得到一个实例Castle.Proxies.IPolicySearchPresenterProxy
而不是FlexPolicySearchPresenterProxy
这给我的 FluorineFx 远程处理应用程序带来了问题。但是,如果我手动创建我的 Castle 代理:
ProxyGenerator generator = new ProxyGenerator();
//My presenter type
Type type = typeof(FlexPolicySearchPresenter);
//My presenter interface
var interfaceType = type.GetInterfaces().Single();
//Get my Interceptor from container. Notice that i had to
//change my Interceptor to implement IInterceptor from Castle libs,
// instead of Ninject IInterceptor
var excepInt = Kernel.Get<ExceptionInterceptor>();
//Manually get all my instances required by my presenter type Constructor
//ideally passed through Constructor Injection
var presenterSearchService = Kernel.Get<IPolicySearchService>();
var userAuthService = Kernel.Get<IUserAuthorizationService>();
//Create proxy, passing interceptor(s) and constructor arguments
var proxy = generator.CreateClassProxy(type, new object[] { presenterSearchService, userAuthService },
new IInterceptor[]
{
excepInt
});
//Ninject.Extensions.Interception.DynamicProxyModule
// I'm using directive ToConstant(..), and not To(..)
//Bind my interface to the new proxy
Bind(interfaceType).ToConstant(proxy).InThreadScope();
var proxy = Kernel.Get<IPolicySearchPresenter>();
返回的类型Castle.Proxies.FlexPolicySearchPresenterProxy
与我的远程实现完美配合。
问题是,我怎样才能让 Ninject.Interception 返回我的实例FlexPolicySearchPresenterProxy
而不是IPolicySearchPresenterProxy
. 请注意,通过手动 Castle 方式,我以不同的方式绑定:
Bind(interfaceType).ToConstant(proxy).InThreadScope();
而不是ninject方式:
Bind<IPolicySearchPresenter>().To<FlexPolicySearchPresenter>().Intercept().With<IExceptionInterceptor>();
我是否需要更改在 Ninject 中进行绑定的方式以获得正确的类型?