我正在为我正在开发的会计系统遵循 DDD 方法。
有两个部分(见下面的域):
- 创建交易:当现金从 Ledger 转移到客户的帐户或反之亦然,或从一个 Ledger 转移到另一个 Ledger 时,将创建一个交易(AccountingTransaction)作为父级(聚合根)。一笔交易可以是多条腿的,即Transaction(AccountingTransaction) 可以有多个LedgerTransaction 或CashTransaction。
- 核对账户:我需要每天在每个分类帐中匹配贷方和借方。
这是我的域的样子:
- AccountingTransaction 是聚合根。
- LedgerTransaction 和 CashTransaction 是 AccountingTransaction 的子实体。
- AccountingTransaction 将有一个 CashTransactions 和 LedgerTransactions 的列表。
- Ledger 是聚合根。
public class AccountingTransaction : AggregateRoot
{
public string AccountingTransactionId { get; private set; }
public TransactionStatus TransactionStatus { get; private set; } //enum
private List<CashTransaction> _cashTransactions = new List<CashTransaction>();
public IReadOnlyList<CashTransaction> CashTransactions => new ReadOnlyCollection<CashTransaction>(_cashTransactions);
private List<LedgerTransaction> _ledgerTransactions = new List<LedgerTransaction>();
public IReadOnlyList<LedgerTransaction> LedgerTransactions => new ReadOnlyCollection<LedgerTransaction>(_ledgerTransactions);
private AccountingTransaction(string transactionId, List<CashTransaction> cashTransactions, List<LedgerTransaction> ledgerTransactions, TransactionStatus transactionStatus)
{
//Code omitted for brevity
}
public static IResult<AccountingTransaction> CreateTransaction(string transactionId, List<CashTransaction> cashTransactions, List<LedgerTransaction> ledgerTransactions, List<ClientAccount> clientAccounts, List<Ledger> ledgerAccounts)
{
//Factory method
//Maintain invaraint and creates a new transaction
}
public IResult CancelTransaction(string username)
{
}
}
public class LedgerTransaction
{
public int Id { get; private set; }
public string LedgerAccountId { get; private set; }
public string TransactionId { get; private set; }
public string EntryDescription { get; private set; }
public DateTime? ReconciledOn { get; private set; }
public TransactionAmount TransactionAmount { get; private set; } //Value object
private LedgerTransaction(int id, TransactionAmount transactionAmount,
string transactionId,
string entryDescription,
string batchId,
string ledgerAccountId)
{
//Code omitted for brevity
}
internal static IResult<LedgerTransaction> CreateTransaction(/*List of arguments*/)
{
//Factory method
//Code omitted for brevity
}
}
public class CashTransaction
{
public int Id { get; private set; }
public string ClientAccountId { get; private set; }
public string TransactionId { get; private set; }
public TransactionAmount TransactionAmount { get; private set; }//Value object
public string EntryDescription { get; private set; }
private CashTransaction(int id,
TransactionAmount transactionAmount,
string transactionId,
string entryDescription,
string clientAccountId)
{
Id = id;
TransactionAmount = transactionAmount;
TransactionId = transactionId;
EntryDescription = entryDescription;
ClientAccountId = clientAccountId;
}
internal static IResult<CashTransaction> CreateTransaction(/*List of arguments*/)
{
//Factory method
//Code omitted for brevity
}
}
public class Ledger : AggregateRoot
{
public string AccountId { get; private set; }
public string Name { get; private set; }
public LedgerType LedgerType { get; set; }
public Currency Currency { get; private set; }
}
所以第一部分(创建交易)工作得很好,我被困在如何处理分类帐账户对账上。
问题:为了核对帐户,对于给定的一天,我需要获取属于特定分类帐的所有分类帐交易,其中 ReconciledOn(请参阅域类)为空。然后我需要确保所有借方和贷方的总和为 0 - 如果不是,我需要报告错误。匹配的借方和贷方也可能属于不同的聚合根 (AccountingTransaction)。这也意味着我需要在聚合根 (AccountingTransaction) 之外获取 Ledger 事务,这违反了 DDD,然后可能直接在 LedgerTransactions 表上执行写入操作。
请告知我应该如何处理这个问题。域类中是否存在缺陷?
我感谢您的帮助。
谢谢!