开发者

NHibernate Multiple Event Listeners

Is it possible to register multiple event listeners?

We currently register event listeners using .ExposeConfiguration(AddSoftDelete) in which AddSoftDelete is a class registering the listener;

private static void AddSoftDelete(Configuration config)
{
    config.SetListener(ListenerType.Delete, new SoftDeleteListener());
}

We have found tha开发者_如何转开发t we cannot register multiple event listeners of the same type, i.e. we cannot register more than one listener for "ListenerType.Delete".

Is it possible to register new listeners without overriding any existing ones?

Solved...

Have managed to register multiple listeners using the following code;

config.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[]
                                                                {
                                                                    new Listener1(),
                                                                    new Listener2()
                                                                };

Repeat for each ListenerType.


The listeners are not actually listeners, they are implementors. There could only be one implementation of an "event".

You could implement a listener where you could plug in several implementations. For instance an implementation for different entity types. You could pass the "event" to each implementation until one of them handles it (eg. when the ISoftDeletable interface is implemented, the SoftDeleteImplementor is handling it). You need to care about competing implementors (more the one could be handling it, the order matters in which you call them).


Why is there a need to register more than one ListenerType.Delete?

If you've got multiple event listeners on one type, there will be some performance issues on your application. If you want to handle different entities with this listener, so do it in your SoftDeleteListener class.


I do something similar in my code. There should be an AppendListeners(ListenerType type, object[] listeners) method on the NHibernate.Cfg.Configuration object.

There's also a SetListeners method which I assume replaces the listener list instead of adding on to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜