I will give two examples. Omit even some important things from DDD now, examples are intended to show the essence of the issue.
How best to do in terms of DDD?
There are two root aggregates, the seller and the announcement. The seller can edit the ad in these examples:
one.
If the models need to reflect real business logic. That Seller changes the ad. changeAdvertName() client layer calls the changeAdvertName() and changeAdvertCost() methods of the Seller aggregate. This gives such an advantage as, say, access checking, we can see that the Seller can only change its Adverts . This is the first option how to do it.
//Client layer call seller.changeAdvertName(name) //AR Seller class Seller{ adverts changeAdvertName(advertId, name){ adverts[advertId].changeName(name) } changeAdvertCost(advertId, cost){ adverts[advertId].changeCost(cost) } } //AR Advert class Advert{ name cost changeName(name){ this.name = name } changeCost(cost){ this.cost = cost } } 2
Another option is where the client layer will call changeName and changeCost directly on the Advert aggregate. I see this implementation more often in various examples.
//Client layer call advert.changeName(name) //AR Advert class Advert{ name cost changeName(name){ this.name = name } changeCost(cost){ this.cost = cost } } Are both options valid in DDD? And which one is more correct and logical in terms of DDD?
changeAdvertCostandchangeAdvertNameclearly redundant methods (since they do not introduce their own logic) - thegetAdvert(id)method in theSellerclass would look more logical. But the excess should be as small as possible - in order not to disperse the attention of those who will be engaged in this code, and accordingly speed up the development + postpone the moment of collapse. - Goncharov Alexander