views:

176

answers:

2

Using NHibernate, what is the best way to handle InsertDate (a.k.a CreateDate) and UpdateDate columns?

For example:

public class Customer 
{
     public virtual string Name { get; set; }
     public virtual DateTime InsertDate { get; set; }
     public virtual DateTime UpdateDate { get; set; }
}

There are probably multiple ways that this could be handle, but could someone who has done this before provide me with some advice.

If it's not obvious, I want InsertDate to be set to the date that a record was inserted and after that to be immutable. UpdateDate needs to be changed every time a record is updated.

Bonus marks if you answer using Fluent Nhibernate.

+1  A: 

Use auditing interceptors for this. A good example can be found here.

zoidbeck
+1  A: 

I've used something similar to Ayende Rahien.

Not so important - just to be complete - my version used Interceptors, not listeners. For more infos to interceptors and listeners read this stackoverflow question.

bernhardrusch