开发者

Help determining aggregate roots

开发者 https://www.devze.com 2023-04-03 10:48 出处:网络
I am stuck in a vortex of analysis paralysis trying to apply DDD to various applications i have either developed in the past or plan on doing in the future.

I am stuck in a vortex of analysis paralysis trying to apply DDD to various applications i have either developed in the past or plan on doing in the future.

Lets take for example a simple softball league. We have leagues, teams that belong to a league (just one), and players that belong to a single team. i know these rules can vary, but lets just pretend.

the way I see this is a team cannot exist without a league and if you delete a league you delete all the teams belonging to it (as well as players), so i have one aggregate here with the root being league. now teams are also an important concept, so does that mean a team could also be its own aggregate root? evans states that when you delete an agg开发者_开发技巧regate root, all its internals delete as well, so this would leave a team without a parent league.

if someone could shed some light i would be very grateful.

thanks


You've asked very similar question at least three times. The domain is always the same: sport teams, leagues and players but the details you provide are always different and this is why you get and accept different answers. You clearly in the state of analysis paralysis. There is very little chance that you would come up with the perfect model the first time. Start writing code and tests and you will get immediate feedback. Look for code smells, refactor and a better model will emerge.

Regarding this version of the question. Aggregate definition:

A cluster of associated objects that are treated as a unit for the purpose of data changes. External references are restricted to one member of the Aggregate, designated as the root. A set of consistency rules applies within the Aggregate's boundaries.

And the classic example is Order as Aggregate root and Order Lines as Entities that are part of the Order Aggregate. Note that Order Lines do not make sense outside of the Order.

In your case the rule that "team cannot exist without a league" is different. It is not enough to proclaim Team to be a part of League Aggregate. It is similar to "Order can not exist without a Customer". It does not mean that the Customer is part of the Order Aggregate. Customer is its own Aggregate, it makes sense on its own. Just like the Team makes sense on its own, it has its own history, fans etc.

This problem can also be approached from 'data-exchange' stand point. In Evan's example the whole Order gets locked so that internal invariants can be enforced. If you make changes to the League would you also want all Teams and Players to be locked as well?

So based on the information you provided this time you need two Aggregates: League and Team. Keep in mind that Aggregates can reference each other.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号