域内的域服务或方法

发布于 2024-11-30 01:35:21 字数 408 浏览 4 评论 0原文

我有两个聚合根:BonusProgram 和 Advertiser。现在的规则是广告商一次只能有一个奖励计划。现在要将bonusProgram分配给广告商,我应该做什么,我在广告商中有这样的方法吗?

    public virtual void AssignBonusProgram(BonusProgram bonusProgram)
    {
        this.bonusProgram = bonusProgram;
    }

或者我是否创建域服务并有这样的方法

   public void SubscribeToBonusProgram(BonusProgram bonusProgram, Advertiser advertiser)
   {

   }

I have two aggrgate Roots, BonusProgram and Advertiser. Now the rule is an advertiser can have only single bonus program at a time. Now to assign bonusProgram to advertiser, what should i do, Do i have method like this in Advertiser.

    public virtual void AssignBonusProgram(BonusProgram bonusProgram)
    {
        this.bonusProgram = bonusProgram;
    }

or do i create domain service and have method like this in

   public void SubscribeToBonusProgram(BonusProgram bonusProgram, Advertiser advertiser)
   {

   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2024-12-07 01:35:21

在这种情况下,服务就有点矫枉过正了。您的业​​务规则已通过以下事实捕获:您的 bonusProgram 不是一个集合。

...规则是广告客户可以在同一时间仅提供单一奖励计划
时间。

您的实施告诉我,广告商可能没有相关的奖金计划。如果不是这种情况,您可以简单地检查 null 并抛出 ArgumentNullException。领域服务经常被滥用,首先尝试将逻辑放入其中一个实体或值类型中可能是个好主意。并且仅在以下情况下使用域服务:逻辑在概念上不属于任何实体。

Service would be an overkill in this case. Your business rule is already captured by the fact that your bonusProgram is not a collection.

... the rule is an advertiser can have only single bonus program at a
time.

And your implementation tells me that it is possible for advertiser to have no associated bonus program. If this is not the case you can simply check for null and throw ArgumentNullException. Domain services are very often misused and it may be a good idea to first try to put logic in one of the entities or value types. And only use domain service if the logic does not belong conceptually to any entity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文