What is the best practice to define the last change and who modified the use of NHibernate?

advertisements

In my application I have a situation where we need to capture the when a record was created and modified and what user performed those actions. So I might have an object something like:

public class Product
{
  int Id;
  int Name;
  DateTime CreatedOn;
  int CreatedByUserId;
  DateTime LastModifiedOn;
  int LastModifiedByUserId;
}

What's the best practice for handling these in NHibernate? Via using an interceptor something like what's described here?


I don't think there's a "best" practice, but the use of event listeners is more common for this. There's a good example at http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx

One thing you'll need to consider is that you need to store that userId somewhere. I'm currently doing that by assigning a static property on the listener on startup. It's not pretty, but it gets the job done.