Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associations' traversal direction

I'm reading the book on Domain Driven Design of Eric Evans - Chapter 5, concerning associations. One of his advices to reduce complexity of a model is to impose a traversal direction for the associations.

I quote:

It is important to constrain relationships as much as possible. A bidirectional association means that both objects can be understood only together. When application requirements do not call for traversal in both directions, adding a traversal direction reduces interdependence and simplifies the design. Understanding the domain may reveal a natural directional bias.

How to chose a traversal direction for an association? Generally, when there is an association between two elements, it may be read and understood in the two directions. What may cause us to chose one direction over the other?

Thanks

like image 351
manash Avatar asked Mar 21 '12 12:03

manash


2 Answers

When there's an association between entity A and entity B, you'll often find yourself using only A.B and never B.A. This may be because A is an aggregate root and is always your starting point, because you already have a reference to its A wherever you manipulate a B, etc.

I guess Evans simply suggests that you should add a traversal direction only when you need it and will use it in the code just after, as opposed to prematurely adding a traversal direction "in case we need it later".

like image 178
guillaume31 Avatar answered Nov 13 '22 19:11

guillaume31


Conceptually all associations are bidirectional. Nevertheless, when implementing them most end up being unidirectional since then you just need to maintain the links in one of the participants.

During design you may want to indicate the navegability to break the bidirectionality at the implementation level and facilitate the coding of the system

like image 4
Jordi Cabot Avatar answered Nov 13 '22 21:11

Jordi Cabot