1

I've a class called

public class AuditFlushEntityEventListener : DefaultFlushEntityEventListener 

This class override OnFlushEntity. So I've a FlushEntityEvent. I was wondering how can I recover the old state object during an update.

Until now I just have my @event.Entity (object of my domain), and a list of objects in @event.EntityEntry.LoadedState

*Nhibernate 3.0, and I'm working with Events cause this and using FlushEntity cause this.

Community
  • 1
  • 1
Custodio
  • 8,594
  • 15
  • 80
  • 115
  • Why do you want to recover the old state during an update ?? – Stefan Steinegger Mar 30 '12 at 05:23
  • To audit the modifications. There is some conceptual problem here? This listener should log the Entities that changed. – Custodio Mar 30 '12 at 12:31
  • 1
    So you don't want to recover the old state, you just want to access the original values. – Stefan Steinegger Mar 30 '12 at 13:38
  • Yes! I managed to do this with `@event.EntityEntry.LoadedState`. But I've a created library to automatically audit changes, and I need two object of my model. I've the left side: `@event.Entity` and I'm trying to figure out how to obtain the right side. Persister can help me in some way? – Custodio Mar 30 '12 at 14:55
  • It's very hard to understand what you mean. What is the left side and the right side? What are these two objects of your model? – Stefan Steinegger Apr 02 '12 at 06:15

1 Answers1

1

The @event.LoadedState contains the previous values in an array, @event.PropertyValues contains the current values. The corresponding persister.PropertyNames contains the property names in the same order and persister.PropertyTypes the NH types.

Example:

for (int i = 0; i < @event.PropertyValues.Length; i++)
{
  Console.WriteLine("Property {0} : {1} => {2}",
    @event.EntityEntry.Persister.PropertyNames[i],
    @event.LoadedState[i],
    @event.PropertyValues[i]);
}

I'm not sure if the "LoadedState" is actually the state that had been loaded. It might be the state in the DB, which may change with every flush. Check this. It may be necessary to take the "first loaded state" you get from the event.

Note: I wrote an "automatic audit trail writer". With complex entities it gets very complicated. Most complicated is the nesting of objects (parent - child). The event is in the depths of NH. You need to handle components and collections.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193