简体   繁体   中英

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 that we cannot register multiple event listeners of the same type, ie 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.

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM