如何将拦截器添加到温莎城堡的类型化工厂设施工厂方法中

发布于 2024-11-02 00:24:06 字数 173 浏览 1 评论 0原文

我正在温莎城堡使用类型化工厂设施。当工厂方法生成它应该创建的实例时,我想得到一个回调,以便连接该实例以获取属性更改通知。这样我就不需要确保在调用工厂方法之后调用这个“创建后”步骤,而是将此责任交给工厂。

有没有办法使用类型化工厂设施或其他 Castle 功能在生成的工厂上注册回调或在工厂上创建用于执行回调的拦截器?

I'm using a Typed Factory Facility in Castle Windsor. I want to get a callback when a factory method generates an instance of what it is supposed to create in order to wire up the instance for property change notification. This way I won't need to ensure this "post-creation" step is called after calling the factory method, instead giving this responsibility to the factory.

Is there a way, either using the Typed Factory Facility or some other Castle feature to register a callback on the generated factory or create an interceptor on the factory which is used to perform the callback?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

淡淡的优雅 2024-11-09 00:24:06

您也许可以通过从 AbstractFacility 派生创建 Facility 来解决此问题。注册到 Kernel.ComponentCreated 事件并检查创建的组件是否需要属性更改通知。如果是这样注册。

您可以使用 ComponentDestroyed 事件来确保您也能很好地注销。下面是我使用 Caliburn.Micro 事件聚合器自动注册视图模型的代码片段

class EventRegistrationFacility : AbstractFacility
{
    private IEventAggregator _eventAggregator;

    protected override void Init()
    {
        Kernel.ComponentCreated += ComponentCreated;
        Kernel.ComponentDestroyed += ComponentDestroyed;
    }

    void ComponentCreated(Castle.Core.ComponentModel model, object instance)
    {
        if (!(instance is IHandle)) return;
        if (_eventAggregator == null) _eventAggregator = Kernel.Resolve<IEventAggregator>();
        _eventAggregator.Subscribe(instance);
    }

    void ComponentDestroyed(Castle.Core.ComponentModel model, object instance)
    {
        if (!(instance is IHandle)) return;
        if (_eventAggregator == null) return;
        _eventAggregator.Unsubscribe(instance);
    }

}

谨致问候,
马尔温。

You can probably solve this by creating a Facility by deriving from AbstractFacility. Register to the Kernel.ComponentCreated event and check whether the created component needs the property changed notification. If so register.

You can use the ComponentDestroyed event to make sure you also nicely unregister. Below a code snippet I use to auto register view models with Caliburn.Micro's event aggregator

class EventRegistrationFacility : AbstractFacility
{
    private IEventAggregator _eventAggregator;

    protected override void Init()
    {
        Kernel.ComponentCreated += ComponentCreated;
        Kernel.ComponentDestroyed += ComponentDestroyed;
    }

    void ComponentCreated(Castle.Core.ComponentModel model, object instance)
    {
        if (!(instance is IHandle)) return;
        if (_eventAggregator == null) _eventAggregator = Kernel.Resolve<IEventAggregator>();
        _eventAggregator.Subscribe(instance);
    }

    void ComponentDestroyed(Castle.Core.ComponentModel model, object instance)
    {
        if (!(instance is IHandle)) return;
        if (_eventAggregator == null) return;
        _eventAggregator.Unsubscribe(instance);
    }

}

Kind regards,
Marwijn.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文