域内的域服务或方法
我有两个聚合根: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,服务就有点矫枉过正了。您的业务规则已通过以下事实捕获:您的
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.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.