Try fast search NHibernate

28 May 2009

Auditing with NHibernate

Provide some good example about how implements Auditing in NHibernate, perhaps, is one of the most difficult thing to do. There are a lot of way to implements the Auditing and, in general, ours examples are oriented to the less useful and the most intrusive; the IAuditable.

public interface IAuditable
DateTime CreatedAt { get; }
string CreatedBy { get; }
DateTime UpdatedAt { get; }
string UpdatedBy { get; }

The implementation of this kind of thing can’t be called “Auditing” because:

  • You can’t know which was the change of an update.
  • It is invasive; any entity is auditable and the Auditing, in general, is outside the scope of a domain-entity.
  • You lost any information after a delete.

For real our examples are not oriented to show you how implement the auditing but only to show you a possible usage of listeners/interceptor.

I’m working in some others implementations in uNhAddIns. My intention is implements from AuditLog to something closer to ParallelModel.

The real challenge is a no intrusive and dynamic implementation of ParallelModel with EventSourcing, perhaps with a “sponsor” it would be faster.

Stay tuned.


  1. You could put DeletedAt and mark the item as deleted with some removed flag, so the application does not consider it anymore.
    It is not the greatest thing to do, but it works with deletes an auiditing :).
    In any case i prefer writing in a separate table and having some way to handle which properties to follow from an entity. In general it is hard to understand what is happening and how it happened, so if putting the calling method, and user really helps.

  2. Your mentioning of parallel model and event sourcing reminded me of something I've been thinking about for quite a while (and I'm certain I'm not the only one): have you considered the possibility that this feature could support other very interesting functionalities, like UNDO or even a full business rules engine? NHibernate already has some infrastructure in place to support it, but another step in this direction could make it immensely powerful.

  3. @Boris
    The first name of the class was "MirrorEntity" because I never understand another kind of Auidit that does not allow undo.
    The Auditing is outside the scope of NH.

  4. Offtopic question: why uNHAddins are not part of NHibernate.Contrib ?

  5. It born before NH Contrib and some classes was ported from uNhAddIns to Burrow.

  6. This comment has been removed by the author.

  7. @Roy
    NH know dirty properties when the entity is not detached.

  8. Thanks for your fast reply Fabio :-) I just realized that the PreUpdateEvent event has all old values of the object aswell the new ones, I will compare them there and do the audit save.

    I will do some tests and see if I can use the IPreUpdateEventListener, IPreInsertEventListener and check for changed values there.

  9. You can do the comparison by your self or ask to NH to give you the list of dirty properties.

  10. Does anyone have sample code using IPostUpdateEventListener, IPostInsertEventListener, IPostDeleteEventListener To wite audit events to an Audit nHibernate Object. I am having many issues with the session. Duplicates, exceptions etc. Thanks