I am attempting to use NHibernate Events to audit my ActiveRecord
objects for changes. My attempts have always resulted in the OldState
on the PreUpdateEvent object is null. Thus, I cannot check if the
State has changed. In the below code, in the FindDirty method,
@event.OldState is null.
Searching the net has helped me construct my sample code, but others
have mentioned this null oldstate problem as well. The answers to fix
the problem have generally referred to NHibernate based solutions (and
I am not sure I understand them), but was wondering if anyone has
ideas how to fix this using ActiveRecord.
Thanks,
Jason
My current sample code
[TestFixture]
public class SampleEventListenerTestFixture : DomainTestFixture<Sample>
{
[Test]
public void Update()
{
var sample = new Sample() { Name = "OldName" };
sample.Save();
sample.Name = "New Name";
sample.Save();
}
}
[EventListener]
public class SampleEventListener : IPreUpdateEventListener,
IPostUpdateEventListener
{
public void OnPostUpdate(PostUpdateEvent @event)
{
var entity = @event.Entity as Sample;
if (entity == null)
return;
}
public Boolean OnPreUpdate(PreUpdateEvent @event)
{
FindDirty(@event);
return false;
}
private static void FindDirty(PreUpdateEvent @event)
{
var dirtyFieldIndexes =
@event.Persister.FindDirty(@event.State, @event.OldState,
@event.Entity, @event.Session);
foreach (var dirtyFieldIndex in dirtyFieldIndexes)
{
var oldValue = @event.OldState[dirtyFieldIndex];
var newValue = @event.State[dirtyFieldIndex];
// Log values to new "AuditLog" object and save appropriately.
}
}
}
[ActiveRecord]
public class Sample : DomainObject<Sample>
{
[Castle.ActiveRecord.Property]
public String Name { get; set; }
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---