首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子实体/值对象之间的DDD关系

子实体/值对象之间的DDD关系
EN

Software Engineering用户
提问于 2018-09-05 11:04:00
回答 1查看 1.3K关注 0票数 1

我对DDD很陌生,并试图通过为应用程序(核心-逻辑层)编写一个原型来解决这个问题。

我的域名模型看起来像这个自动取款机。(我留下了问题所不需要的部分):

因此,我们有一个集合根DebitAccount,它分配了Estimations (关于在给定时间间隔内发生的成本/收入)。它还指定了在给定日期实际发生(成本/收入)的Postings

Posting可能会给出一个估计值(例如,“我们有每月支付租金的费用”),但它也可能是看不见的(“我的笔记本坏了,所以我需要买一个新的)。”

以下是实现该模型所需的(imho)代码:

代码语言:javascript
复制
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;
    }
}

我面临着几个问题:

  1. 当调用借方帐户的Post-Method时,如何获得该帐户所属的Estimation (如果有的话)?在应用程序代码中,可以通过公共属性获取所有估计值,找到正确的估计值,并将其分配给当前的公告。但这需要客户知道,他必须在财产中寻找估计值。
  2. 下一个问题类似于第一个问题:当传输到另一个DebitAccount时,如何找到相应的Estimation (如果有的话)?
  3. TransferTo-Function:DebitAccounts在哪里以及如何持久化。调用代码(例如,应用层)是否假定已传递的DebitAccount(to)被更改(按引用调用),并将两者都保存在回购中?

如何解决这些问题?或者我错了我的DDD设计有缺陷?任何想法、评论和想法都会受到赞赏:)

EN

回答 1

Software Engineering用户

回答已采纳

发布于 2018-09-06 14:01:24

我认为从发布到估计的直接对象引用有点问题。

如果Posting应该是一个纯值对象,那么它应该是不可变的。因此,如果不指定其所属的估计值,您就不应该能够创建公告。因此,每当需要创建公告时,还需要访问一些DebitAccount及其估计值。这听起来像是大量的控制和逻辑从DebitAccount类中泄漏出来。

如果只有一个DebitAccount知道它的估计和发布是如何相互关联的呢?那么,就没有必要从帖子中引用估计数,甚至不需要从估计数到帖子引用。

我认为这将解决问题1和2。

关于问题3:我可能更喜欢方法AccountService.ExecuteTransaction(Transaction)而不是方法DebitAccount.TransferAmountTo(DebitAccount, Amount amount, string transfername)。事务可以是一个值对象,如

代码语言:javascript
复制
class Transaction : ValueObject {
    private Id fromAccountId;
    private Id toAccountId;
    private Amount amount;
    private string name;
    ...
}

Execute方法随后将更新两个帐户,并确保所有内容都得到正确的持久化。

票数 1
EN
页面原文内容由Software Engineering提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://softwareengineering.stackexchange.com/questions/377974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档