views:

141

answers:

1

Hi,

How do I add a new entity object using NHibernate whilst handling a PostUpdateEvent event? I have implemented an audit handler which essentially creates a new object in the OnPostUpdate handler, and attempts to insert into the database.

However, it doesn't actually seem to get committed at any point. If I start a new transaction and commit that in the OnPostUpdate handler, it recurses and gives a stack overflow.

Any ideas?

Thanks,

Jim

A: 

I think that PostUpdateEvent is too late in the processing pipeline for what you want. Examples I've seen (e.g. http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx and http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx) use PreUpdateEvent. If you have a database transaction open then you will be sure that the audit details are written only when the main update succeeds, even though you may use pre-update and not post-update events.

Cheers, John

John Rayner