15

Looking at all the possibilites of creation / update columns in NHibernate I mostly (Stackoverflow question, Ayende Rahien) see solutions with Listeners.

The programmer who was programming this in my company used an Interceptor to achieve the same thing.

Is there any difference between those two solutions? (Is one of them obsolete, is one of them preferred and what are the advantages and / or disadvantages)

Community
  • 1
  • 1
bernhardrusch
  • 11,670
  • 12
  • 48
  • 59

2 Answers2

18

Interceptors are the the old way, event-listeners are newer and server the same purpose. So in a new project, event-listeners are recommended. You hook up to the new NHibernate event system.

Edit: As Rashack mentions in his comment, there are some operations that can only be done using the interceptor.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • 15
    That's not entirely true. Although event listeners are newer and recomended there are certain operations that you cannot do with them. Namely providing your way to instatiate objects and capturing transaction start/ends. – Rashack May 15 '09 at 08:06
0

As per documentation, it states that : "If you have to react to particular events in your persistence layer, you can also use the Hibernate event architecture. The event system can be used in addition, or as a replacement, for interceptors. "

There are a few differences in both :

  1. Listeners are easily configurable i.e. as per hibernate doc "Having the type additionally defined during registration makes it easier to turn custom listeners on or off during configuration"

  2. Interceptors can be session-scoped or session factory scoped through programmatic as well as declarative approach. On the other hand, Listeners registered declaratively cannot share instances. If the same class name is used in multiple elements, each reference will result in a separate instance of that class. If you need to share listener instances between listener types you must use the programmatic registration approach.

Satyam
  • 703
  • 6
  • 20