title: Audit many-to-many relationship in NHibernate
body: <p>I have implemented listeners to audit changes to tables in my application using <code>IPreUpdateEventListener</code> and <code>IPreInsertEventListener</code> and everything works except for my many-to-many relationships that don't have additional data in the joining table (i.e. I don't have a POCO for the joining table).</p>
  
  <p>Each auditable object implements an <code>IAuditable</code> interface, so the event listener checks to see if a POCO is of type <code>IAuditable</code>, and if it is it records any changes to the object.  Look up tables implement an <code>IAuditableProperty</code> inteface, so if a property of the <code>IAuditable</code> POCO is pointing to a lookup table, the changes are recorded in the log for the main POCO.</p>
  
  <p>So, the question is, how should I determine I'm working with a many-to-many collection and record the changes in my audit table?</p>
  
  <p>Edit: I'm using NHibernate 2.1.2.4000</p>
  
  <pre><code>//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) &amp;&amp; (newState[i] is IList&lt;object&gt; || 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(" =&gt; ");
          }
          changes.Append(((IAuditableProperty)newState[i]).AuditPropertyValue);
      }
      else
      {
          //Record changes for primitive values
          if(isUpdateEvent)
          {
              changes.Append(oldState[i])
                  .Append(" =&gt; ");
          }
          changes.Append(newState[i]);
      }
      changes.AppendLine();
  }
  </code></pre>