title: C# - Should an object be responsible for creating a history object when it changes something like status?
body: <p>This is more of an architecture/best practices question than anything else, so please feel free to add your two cents. I know i stated status in the title, but this goes for any basic property of an object.  I think the account example below will help demonstrate my question a little better than status.</p>
  
  <p>Here is a sample Account object:</p>
  
  <pre><code>public class Account
  {
     private IList&lt;Transaction&gt; _transactions;
  
     public AddTransaction(trans as Transaction)
     {
        _transaction.add(trans)
     }
  }
  </code></pre>
  
  <p>Now lets say I want to start keeping a history of every time a transaction is added with this object.</p>
  
  <pre><code>public class AccountHistory
  {
     private DateTime _historyDate;
     private String _details;
  
     public AccountHistory(string details)
     {
        _historyDate = DateTime.Now;
        _details = details;
     }
  }
  </code></pre>
  
  <p>At this level what I would normally do is add a collection of history events to the account object and also add a line of code to create a history event inside of the AddTransaction() method like this</p>
  
  <pre><code>public AddTransaction(trans as Transaction)
  {
     _transaction.add(trans);
     **_historyEvents.add(new AccountHistory("Transaction Added: " + trans.ToString());**
  }
  </code></pre>
  
  <p>Now the next part is where the problem starts to arise.  Suppose I want to do a bulk posting and I want to retain a record of which accounts were changed in this bulk posting for something like a report or if I needed to undo it later. So I would create an object like this.</p>
  
  <pre><code>public class HistoryGroup()
  {
     private IList&lt;AccountHistory&gt; _events;
  }
  </code></pre>
  
  <p>From here I see a few different options to handle this since it can't be handled by the example code above.</p>
  
  <p>1)  Create a function in a Service type object that loops through a list of accounts calling the AddTransaction() method and also creating history records tied to a HistoryGroup</p>
  
  <pre><code> public void AddTransactions(IList&lt;Account&gt; accounts, Transaction trans)
      {
         HistoryGroup history = new HistoryGroup(); 
         for (int x=0;x &lt;=accounts.Count - 1; x++)
         {
           accounts(x).AddTransaction(trans);
           history.AddEvent(new AccountHistory("Added Transaction: " + trans.ToString();
         }
      }
  </code></pre>
  
  <p>2)  Pass some type of HistoryManager object into the AddTransaction method along with the transaction to be added. Then the function could use the history manager to create the records.</p>
  
  <p>Ok this post is long enough. If i've not been clear enough let me know.  Thanks for you input.</p>