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; }
}