2

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

Wozart
  • 125
  • 1
  • 7

3 Answers3

0

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.

Timur Zanagar
  • 323
  • 3
  • 21
  • I agree, this was as an example only. The question is related to registering multiple listeners of the same type. If it helps, let's assume the listener is a different type, i.e. PostUpdate, PreInsert etc – Wozart Feb 28 '11 at 07:27
  • OK. Just add another line with this "config.SetListener(ListenerType.Delete, new SoftDeleteListener());". That's it. – Timur Zanagar Feb 28 '11 at 08:05
0

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.

Jason Freitas
  • 1,587
  • 10
  • 18
0

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

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • This made sense to me, a very good explanation which directed my thoughts onto the right track! – Wozart Feb 28 '11 at 13:18