如何在 OOP 中建立类似银行账户的模型?
我正在使用 WPF 构建一个现金管理软件用于学习目的,但在正确建模现金帐户以便我可以在每次交易后看到余额时遇到一些麻烦。
这是我现在拥有的内容的摘要版本:
帐户类:
public class Account {
public long Id { get; set; }
public string Name { get; set; }
public decimal StartingBalance { get; set; }
}
类别类:
public class Category {
public long Id { get; set; }
public string Name { get; set; }
}
事务类:
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; }
}
我想要实现的是,仅使用 WPF 绑定功能,填充数据网格并查看给定日期间隔的以下数据和帐户:
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
我希望能够选择“所有帐户”选项,并在余额栏中查看所有帐户余额的总和。
到目前为止,代码工作正常,但我在数据网格中没有“余额”字段,也看不到对此进行建模的优雅方法,我需要你的帮助!
非常感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我这样做时,我倾向于简单地从交易和日记账开始(日记账是交易的集合)。您拥有日记帐,以便可以冲销一组交易。
我的交易表往往类似于:
那么您基本上就拥有了一个非常灵活的系统。每一次操作都可以逆转,CR/DR暗示方向,您可以通过拥有不同的账户和账户类型来管理您的所有资金。您可以根据帐户缓存帐户的当前值,并且可以根据需要从日记帐重新计算。
-- 编辑:
就运行余额而言,如果不清楚,我将在帐户表中设置一个字段,例如“CurrentBalance”,并通过触发器(或通过公共代码内,可能排队)进行调整情况,取决于各种其他因素。重点是您在“交易”事件上更新它,而不是每次都计算它。
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.
运行余额是您的关键问题。它不属于模型中的任何地方。您需要的是ViewModel。搜索 WPF 实现中常见的 MVVM 或 Model-View-ViewModel 模式。
基本上,您需要创建一个专门的
Transaction
类(可能称为RegisterTransaction
),其中包含每个交易的运行余额字段。您需要为每笔交易计算它。从那里,使用 WPF 绑定到您的RegisterTransaction
对象。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 calledRegisterTransaction
), 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 yourRegisterTransaction
objects.