I used listeners to audit table changes in my application using IPreUpdateEventListener and IPreInsertEventListener , and everything works, except for my many-to-many relationships that don't have additional data in the join table (i.e. I have POCO for join tables).
Each object being scanned implements the IAuditable interface, so the event listener checks whether the POCO type is IAuditable and if it records any changes to the object. The lookup tables implement the inteface IAuditableProperty , so if the IAuditable POCO property points to the lookup table, the changes are logged for the main POCO.
So the question is, how should I determine that I am working with a many-to-many collection and recording changes to my audit table?
Edit: I am using NHibernate 2.1.2.4000
//first two checks for LastUpdated and LastUpdatedBy ommitted for brevity else if (newState[i] is IAuditable) { //Do nothing, these will record themselves separately } else if (!(newState[i] is IAuditableProperty) && (newState[i] is IList<object> || newState[i] is ISet)) { //Do nothing, this is a collection and individual items will update themselves if they are auditable //I believe this is where my many-to-many values are being lost } else if (!isUpdateEvent || !Equals(oldState[i], newState[i]))//Record only modified fields when updating { changes.Append(preDatabaseEvent.Persister.PropertyNames[i]) .Append(": "); if (newState[i] is IAuditableProperty) { //Record changes to values in lookup tables if (isUpdateEvent) { changes.Append(((IAuditableProperty)oldState[i]).AuditPropertyValue) .Append(" => "); } changes.Append(((IAuditableProperty)newState[i]).AuditPropertyValue); } else { //Record changes for primitive values if(isUpdateEvent) { changes.Append(oldState[i]) .Append(" => "); } changes.Append(newState[i]); } changes.AppendLine(); }
c # nhibernate audit many-to-many
Kendrick
source share