title: IsDirty Flag Pattern
body: <p>I have a base class for a BLL that includes the following function:</p>
  
  <pre><code>    public bool IsDirty { get; protected set; }
  
      internal void SetField&lt;TParam&gt;(ref TParam field, TParam value)
      {
          if (EqualityComparer&lt;TParam&gt;.Default.Equals(field, value) == false)
          {
              field = value;
              IsDirty = true;
          }
      }
  </code></pre>
  
  <p>In the classes that inherit the base class I use this as a wrapper around the SET object, such as: </p>
  
  <pre><code>    public string UserName
      {
          get { return _userName; }
          set { SetField(ref _userName, value); }
      }
  </code></pre>
  
  <p>I use the IsDirty property to test to see if I need to issue an Update. If at least one of the properties changes then save to database. This works for most types but Collections and Lists can change without using set. I wrote a wrapper for the Collection to have an IsDirty flag on the List which could be tested for change: </p>
  
  <pre><code>    public class CollectionChangeTracked&lt;T&gt; : Collection&lt;T&gt;
      {
          public bool IsDirty {get; set;}
  
          public CollectionChangeTracked()
          {
              IsDirty = false;
          }
  
          protected override void InsertItem(int index, T newItem)
          {
              base.InsertItem(index, newItem);
              IsDirty = true; 
          }
  
          protected override void SetItem(int index, T newItem)
          {
              base.SetItem(index, newItem);
              IsDirty = true;
          }
  
          protected override void RemoveItem(int index)
          {
              base.RemoveItem(index);
              IsDirty = true;
          }
  
          protected override void ClearItems()
          {
              base.ClearItems();
              IsDirty = true;
          }
      }
  }
  </code></pre>
  
  <p>The problem is that I now have to test both the Classe's IsDirty property and any CollectionChangeTracked.IsDirty flags for updates. I could create method that performs the test in one spot such as:</p>
  
  <pre><code>    public CollectionChangeTracked&lt;ApplicationRole&gt; RolesList
      {
          get { return _rolesList; }
          set { SetField(ref _rolesList, value); }
      }
  
      public override bool IsDirty
      {
          get { return ResolveIsDirty(); }
          protected set { _isDirty = value; }
  
      private bool ResolveIsDirty()
      { 
          bool returnValue;
  
          if (_isDirty || RolesList.IsDirty)
              returnValue = true;
          else
              returnValue = false;
  
          return returnValue;
      }
  </code></pre>
  
  <p>But it seems like I should be able to come up with a cleaner solution that would allow a Class that contains the Collection to subscribe to a change of IsDirty of the CollectionChangeTracked object and update the IsDirty based on that change. Is this the better approach and how would I implement? </p>