Although it doesn't track the full history of an entity, another option for auditing is to record information about the entity's creation and the most recent change directly in the entity. In this recipe, I'll show you how to use NHibernate's events to create and change stamp entities.
Create a new class library project named
Changestamp
.Add a reference to
NHibernate.dll
.Create an interface named
IStampedEntity
with the following code:public interface IStampedEntity { string CreatedBy { get; set; } DateTime CreatedTS { get; set; } string ChangedBy { get; set; } DateTime ChangedTS { get; set; } }
Create an interface named
IStamper
with the following code:public interface IStamper { void Insert(IStampedEntity entity, object[] state, IEntityPersister persister); void Update(IStampedEntity entity, object[] oldState, object[] state, IEntityPersister persister); }
Create a new
EventListener
class as follows:public class...