温莎城堡:连接容器的解析和释放机制
我正在尝试当 IoC 容器创建侦听器时,实现侦听器自动注册到单例事件聚合器 - 基本上就是 Jeremy D. Miller 正在做的事情,但使用 Castle 而不是 StructureMap。
因此,我希望能够“拦截”Windsor 的对象创建机制,并且如果该对象支持标记接口(例如 IListener
),则调用 Subscribe
方法来 < code>EventAggregator(也在容器中注册)使新创建的对象成为事件的订阅者。另外,在容器释放对象实例之前,我希望能够取消订阅它。
我对温莎城堡中应该使用什么机制来实现这样的事情有点困惑?我开始查看 IInterceptor
接口,但它似乎拦截对对象的所有调用,这不是我真正需要的(并且出于性能原因希望避免)。
I'm trying to implement automatic registration of my listeners to a singleton event aggregator when listeners are created by the IoC container - basically what Jeremy D. Miller is doing, but with Castle instead of StructureMap.
So I want to be able to "intercept" Windsor's object creation mechanism and, if the object supports the marker interface (let's say IListener
), call the Subscribe
method to an EventAggregator
(which is also registered in the container) to make the newly created object a subscriber to events. Also, before the object instance has been released by the container, I want to be able to unsubscribe it.
I'm a little bit confused about what mechanism in Windsor Castle I should use to achieve something like this? I started looking at IInterceptor
interface, but it seems to intercept all calls to the object, which is not what I really need (and want to avoid for performance reasons).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IKernel
公开了各种事件,例如ComponentCreated
和ComponentDestroyed
,您可以使用它们来构建它。网络上有许多示例。否则,您可以只使用事件接线工具,但这不是惯例基于。
IKernel
exposes various events likeComponentCreated
andComponentDestroyed
which you can use to build that. There are many samples on the web.Otherwise you could just use the event wiring facility, but it's not convention based.
您还可以像这样使用 OnCreate:
You could also use OnCreate like this: