温莎城堡:连接容器的解析和释放机制

发布于 2024-08-02 18:10:53 字数 438 浏览 5 评论 0原文

我正在尝试当 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春夜浅 2024-08-09 18:10:53

IKernel 公开了各种事件,例如 ComponentCreatedComponentDestroyed,您可以使用它们来构建它。网络上有许多示例

否则,您可以只使用事件接线工具,但这不是惯例基于。

IKernel exposes various events like ComponentCreated and ComponentDestroyed 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.

撩动你心 2024-08-09 18:10:53

您还可以像这样使用 OnCreate:

container.Register(
        Component.For(typeof (Foo)).OnCreate(
          (k, c) => {
            // ...
            eventAggregator.Subscribe(c);
            // ...
          }));

You could also use OnCreate like this:

container.Register(
        Component.For(typeof (Foo)).OnCreate(
          (k, c) => {
            // ...
            eventAggregator.Subscribe(c);
            // ...
          }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文