在温莎城堡中,我可以注册接口组件并获取实现的代理吗?
让我们考虑一些情况:
_windsor.Register(Component.For<IProductServices>().ImplementedBy<ProductServices>().Interceptors(typeof(SomeInterceptorType));
在这种情况下,当我请求 IProductServices 时,windsor 将代理该接口来拦截接口方法调用。 如果我这样做:
_windsor.Register(Component.For<ProductServices>().Interceptors(typeof(SomeInterceptorType));
那么我不能要求温莎解析 IProductServices,而是要求 ProductServices,它将返回一个将拦截虚拟方法调用的动态子类。 当然,动态子类仍然实现“IProductServices”
我的问题是:我可以像第一种情况一样注册接口组件,并像第二种情况一样获取子类代理吗?
我想要这个有两个原因:
1 - 因为要解析的代码无法了解 ProductServices 类,只能了解 IProductServices 接口。 2 - 因为某些将发送者作为参数传递的事件调用将传递 ProductServices 对象,并且在第一种情况下,该对象是动态代理上的字段,而不是 Windsor 返回的真实对象。让我举一个例子来说明这如何使事情变得复杂:假设我有一个自定义集合,当其项目通知属性更改时,该集合会执行某些操作:
private void ItemChanged(object sender, PropertyChangedEventArgs e)
{
int senderIndex = IndexOf(sender);
SomeActionOnItemIndex(senderIndex);
}
如果我添加了接口代理,则此代码将失败,因为发送者将是接口代理和 IndexOf(sender) 将返回 -1。
Lets consider some cases:
_windsor.Register(Component.For<IProductServices>().ImplementedBy<ProductServices>().Interceptors(typeof(SomeInterceptorType));
In this case, when I ask for a IProductServices windsor will proxy the interface to intercept the interface method calls.
If instead I do this :
_windsor.Register(Component.For<ProductServices>().Interceptors(typeof(SomeInterceptorType));
then I cant ask for windsor to resolve IProductServices, instead I ask for ProductServices and it will return a dynamic subclass that will intercept virtual method calls.
Of course the dynamic subclass still implements 'IProductServices'
My question is : Can I register the Interface component like the first case, and get the subclass proxy like in the second case?.
There are two reasons for me wanting this:
1 - Because the code that is going to resolve cannot know about the ProductServices class, only about the IProductServices interface.
2 - Because some event invocations that pass the sender as a parameter, will pass the ProductServices object, and in the first case this object is a field on the dynamic proxy, not the real object returned by windsor. Let me give an example of how this can complicate things : Lets say I have a custom collection that does something when their items notify a property change:
private void ItemChanged(object sender, PropertyChangedEventArgs e)
{
int senderIndex = IndexOf(sender);
SomeActionOnItemIndex(senderIndex);
}
This code will fail if I added an interface proxy, because the sender will be the field in the interface proxy and the IndexOf(sender) will return -1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以:
Yes, you can: