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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With