Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best practice for setting last modified and who modified using NHibernate?

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?

like image 438
Mark Boltuc Avatar asked Nov 15 '22 12:11

Mark Boltuc


1 Answers

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.

like image 177
Diego Mijelshon Avatar answered Dec 19 '22 21:12

Diego Mijelshon