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.
Here is a sample Account object:
public class Account
{
private IList<Transaction> _transactions;
public AddTransaction(trans as Transaction)
{
_transaction.add(trans)
}
}
Now lets say I want to start keeping a history of every time a transaction is added with this object.
public class AccountHistory
{
private DateTime _historyDate;
private String _details;
public AccountHistory(string details)
{
_historyDate = DateTime.Now;
_details = details;
}
}
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
public AddTransaction(trans as Transaction)
{
_transaction.add(trans);
**_historyEvents.add(new AccountHistory("Transaction Added: " + trans.ToString());**
}
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.
public class HistoryGroup()
{
private IList<AccountHistory> _events;
}
From here I see a few different options to handle this since it can't be handled by the example code above.
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
public void AddTransactions(IList<Account> accounts, Transaction trans)
{
HistoryGroup history = new HistoryGroup();
for (int x=0;x <=accounts.Count - 1; x++)
{
accounts(x).AddTransaction(trans);
history.AddEvent(new AccountHistory("Added Transaction: " + trans.ToString();
}
}
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.
Ok this post is long enough. If i've not been clear enough let me know. Thanks for you input.