I decided to implement the event listeners in the latest build of NHibernate to keep track of who is making edits and what those edits are. My question is this - the below does work and I can step through it but what I'm not sure how these changes get saved ... do I need to build an audit table and write a mapping for it to call a save or what method is best to get the state of the object along w/ the "updated by" and "updated date" information so I can bring this up for someone at a later date.
I thought the base class provided this (or a subset of this functionality) but I can't seem to find a good blog post on what I'm missing here. Any help would be much appreciated!
Imports NHibernate.Event
Imports NHibernate.Event.Default
Public Class CustomSaveEventListener
Inherits DefaultSaveEventListener
Protected Overloads Overrides Function PerformSaveOrUpdate(ByVal evt As SaveOrUpdateEvent) As Object
Dim entity As IEntity = TryCast(evt.Entity, IEntity)
If entity IsNot Nothing Then
ProcessEntityBeforeInsert(entity)
End If
Return MyBase.PerformSaveOrUpdate(evt)
End Function
Friend Overridable Sub ProcessEntityBeforeInsert(ByVal entity As IEntity)
Dim user As User = DirectCast(Thread.CurrentPrincipal, User)
entity.ModifiedBy = user.UserName
entity.ModifiedDate = DateTime.Now
End Sub
End Class
When I open Reflector I see the below for this base class method - but what is it doing exactly?
protected override object PerformSaveOrUpdate(SaveOrUpdateEvent @event)
{
EntityEntry entry = @event.Session.PersistenceContext.GetEntry(@event.Entity);
if ((entry != null) && (entry.Status != Status.Deleted))
{
return this.EntityIsPersistent(@event);
}
return this.EntityIsTransient(@event);
}