4

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.

cbp
  • 25,252
  • 29
  • 125
  • 205

2 Answers2

2

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

zoidbeck
  • 4,091
  • 1
  • 26
  • 25
2

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.

Community
  • 1
  • 1
bernhardrusch
  • 11,670
  • 12
  • 48
  • 59