Given a structure for say, a bank account..
class Account
{
virtual int Id { get; set; }
virtual int Balance { get; set; }
}
And I want to track transactions done, so say a simple class…
class Transaction
{
virtual int Id { get; set; }
virtual Account Account { get; set; }
virtual DateTime Timestamp { get; set; }
virtual int Amount { get; set; }
}
Let’s assume I want to keep track of transactions done, which is the more intelligent approach here?
interface IAccountRepository
{
void Deposit(int account, int amount)
}
or …
class Account
{
void Deposit(int amount)
{
// this one is easier, but then I have to repeat
// myself because I need to store the Transaction
// in the database too.
}
}
The Repository pattern seems to be the most encompassing, since it will have a handle to the unit of work/orm/session (using nHibernate) – but using a class-level method seems more straightforward, since it’s more in line with standard object oriented principle of ‘Do this to this object’.
My question is that if I want to log the transactions, then I have to make sure they get saved as database objects too. Going the second route, with a class level method, I can’t do this inside of the Account class, so I would end up having to repeat myself.
My other option is another abstraction..
interface ITransactionRepository
{
void CreateTransaction(int account, int amount);
}
Which works fine, It kind of wraps A and B together because I would find the account in the TransactionRepository and then perform its Deposit method, but it doesn’t really feel like this is a wise approach. I don’t know why, my gut just tells me it isn’t the best way to go.
This applies to more than just this one set of classes, of course – it’s a principle of design. I wanted to see what more veteran programmers would do in this situation, if you have any thoughts.
I would sugguest using the repository pattern for CRUD (Create, read, update, delete) operations on the Accounts.
Then put the Deposit method in the Account class like you mentioned
Then you access the account through the repository to update the account
Transations could be done in a similar way, but I would probably store the account id, rather than the Account instance in the Transcation.