domain service or method inside domain
开发者_Python百科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)
{
}
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.
精彩评论