【问题标题】:How can an interceptor be added to a Typed Factory Facility factory method in Castle Windsor如何将拦截器添加到 Castle Windsor 中的 Typed Factory Facility 工厂方法中
【发布时间】:2011-04-15 19:41:27
【问题描述】:

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

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

【问题讨论】:

  • 工厂上的普通​​拦截器不行吗?

标签: .net castle-windsor typed-factory-facility


【解决方案1】:

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

您可以使用 ComponentDestroyed 事件来确保您也可以很好地取消注册。下面是我用来向 Caliburn.Micro 的事件聚合器自动注册视图模型的代码 sn-p

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);
    }

}

亲切的问候, 马尔维恩。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    相关资源
    最近更新 更多