Reputation: 2191
I'm building a cash management software using WPF for learning purposes, and I'm having some troubles to properly model a cash account so that I can see the balance, after each transaction.
Here's a summarized version of what I have now:
An account class:
public class Account {
public long Id { get; set; }
public string Name { get; set; }
public decimal StartingBalance { get; set; }
}
A category class:
public class Category {
public long Id { get; set; }
public string Name { get; set; }
}
A transaction class:
public class Transaction {
public long Id { get; set; }
public DateTime Date { get; set; }
public Account Account { get; set; }
public Category Category { get; set; }
public string Description { get; set; }
public decimal TransactionValue { get; set; }
}
What I want to achive is, using only the WPF binding capabilities, populate a datagrid and view the following data for a given date interval and account:
Date Account Category Description Value Balance
02/02/10 A1 C1 D1 22.30 230.00
02/03/10 A1 C1 D2 -30.00 200.00
And I would like to be able to select an option "All Accounts" and see in the balance column the sum of the balance of all accounts.
The code is working fine until now, but I don't have the Balance field in the datagrid neither can see an elegant way to model this, I need your help guyz!
Thanks a lot for the help.
Upvotes: 4
Views: 1909
Reputation: 55072
When I do this, I tend to start simply with Transactions and Journals (a Journal being a collection of transactions). You have journals so that you can reverse a group of transactions.
My transaction table tends to be something like:
Then you've basically got a very flexible system. Every operation can be reversed, the CR/DR implies the direction, and you can manage all your funds by having different accounts and account types. You cache the current value of an account against the account, and can recalculate from the journals if so desired.
-- Edit:
Just in relation to the running balance, incase it's not clear, I would have a field against the Account table, such as "CurrentBalance", and adjust that via triggers (or via a common in-code, perhaps queued) situation, depending on various other factors. The point being that you update it on a "transaction" event, you don't calculate it each time.
Upvotes: 3
Reputation: 10389
The running balance is your key problem here. It doesn't belong in the model anywhere. What you need is called a ViewModel. Search around for the MVVM or Model-View-ViewModel pattern which is common in WPF implementations.
Basically, you need to create a specialized Transaction
class (maybe called RegisterTransaction
), which contains a field for the running balance for each transaction. You will need calculate it for each transaction. From there, use WPF to bind to your RegisterTransaction
objects.
Upvotes: 1