Need help with NHibernate Events, ActiveRecord and OldState is null

346 views
Skip to first unread message

jsmorris

unread,
Aug 4, 2009, 8:03:59 PM8/4/09
to castle-pro...@googlegroups.com
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; }
}

Zdeslav Vojkovic

unread,
Aug 5, 2009, 8:11:08 AM8/5/09
to castle-pro...@googlegroups.com
Have you tried calling SaveCopy instead of Save?

jsmorris

unread,
Aug 6, 2009, 5:31:56 PM8/6/09
to castle-pro...@googlegroups.com
Zdeslav, thanks for the suggestion, but I think my testing framework
was not set up properly. I tried my experiment on my working
application and it produced the old and new states. I am going to
have to examine my testing framework to see what is missing to enable
unit testing my event listeners.

Anybody have a good example of testing nhibernate event listeners?

Jason

Markus Zywitza

unread,
Aug 7, 2009, 12:15:24 AM8/7/09
to castle-pro...@googlegroups.com
To test the listeners themselves, simply new them and call the method in the interface. The NH event classes are straightforward, see their sourcecode.

To test that the listeners are included, see the tests in AR.

-Markus

2009/8/6 jsmorris <jsmo...@gmail.com>

Adam Swiech

unread,
Aug 14, 2009, 7:59:52 AM8/14/09
to Castle Project Users
Try do something like this:

[TestFixture]
public class SampleEventListenerTestFixture :
DomainTestFixture<Sample>
{
[Test]
public void Update()
{
using (new SessionScope())
{
Reply all
Reply to author
Forward
0 new messages