我对DDD很陌生,并试图通过为应用程序(核心-逻辑层)编写一个原型来解决这个问题。
我的域名模型看起来像这个自动取款机。(我留下了问题所不需要的部分):

因此,我们有一个集合根DebitAccount,它分配了Estimations (关于在给定时间间隔内发生的成本/收入)。它还指定了在给定日期实际发生(成本/收入)的Postings。
Posting可能会给出一个估计值(例如,“我们有每月支付租金的费用”),但它也可能是看不见的(“我的笔记本坏了,所以我需要买一个新的)。”
以下是实现该模型所需的(imho)代码:
public class DebitAccount : BaseEntity
{
protected ICollection<Estimation> _Estimations;
protected ICollection<Posting> _Postings;
public virtual IEnumerable<Estimation> Estimations
{
get
{
return new List<Estimation>(_Estimations);
}
}
public virtual IEnumerable<Posting> Postings
{
get
{
return new List<Posting>(_Postings);
}
}
public void Post(Posting posting)
{
var associatedEstimation = _Estimations.FirstOrDefault(e => e == posting.BelongsToEstimation);
if(associatedEstimation == null)
{
var now = _DateProvider.GetCurrentDate();
var guid = Guid.NewGuid();
associatedEstimation = new Estimation(new Posting[] { posting }, new Amount(0, _CurrencyProvider.GetDefaultCurrency()), posting.PostingName, new DayInterval(now, now, 0),new EstimationId(guid));
_Estimations.Add(associatedEstimation);
}
_Postings.Add(posting);
}
public DebitAccount TransferAmountTo(DebitAccount to, Amount amount, string transferName)
{
var currentDate = _DateProvider.GetCurrentDate();
var fromPosting = new Posting(amount.Invert(),transferName, currentDate, null);
var toPosting = new Posting(amount.Clone() ,transferName, currentDate, null);
this.Post(fromPosting);
to.Post(toPosting);
return this;
}
}我面临着几个问题:
Post-Method时,如何获得该帐户所属的Estimation (如果有的话)?在应用程序代码中,可以通过公共属性获取所有估计值,找到正确的估计值,并将其分配给当前的公告。但这需要客户知道,他必须在财产中寻找估计值。DebitAccount时,如何找到相应的Estimation (如果有的话)?TransferTo-Function:DebitAccounts在哪里以及如何持久化。调用代码(例如,应用层)是否假定已传递的DebitAccount(to)被更改(按引用调用),并将两者都保存在回购中?如何解决这些问题?或者我错了我的DDD设计有缺陷?任何想法、评论和想法都会受到赞赏:)
发布于 2018-09-06 14:01:24
我认为从发布到估计的直接对象引用有点问题。
如果Posting应该是一个纯值对象,那么它应该是不可变的。因此,如果不指定其所属的估计值,您就不应该能够创建公告。因此,每当需要创建公告时,还需要访问一些DebitAccount及其估计值。这听起来像是大量的控制和逻辑从DebitAccount类中泄漏出来。
如果只有一个DebitAccount知道它的估计和发布是如何相互关联的呢?那么,就没有必要从帖子中引用估计数,甚至不需要从估计数到帖子引用。
我认为这将解决问题1和2。
关于问题3:我可能更喜欢方法AccountService.ExecuteTransaction(Transaction)而不是方法DebitAccount.TransferAmountTo(DebitAccount, Amount amount, string transfername)。事务可以是一个值对象,如
class Transaction : ValueObject {
private Id fromAccountId;
private Id toAccountId;
private Amount amount;
private string name;
...
}Execute方法随后将更新两个帐户,并确保所有内容都得到正确的持久化。
https://softwareengineering.stackexchange.com/questions/377974
复制相似问题