1

I've been struggling with modeling something out correctly, the DDD way.

The use case is quite simple. You're a User, and you can be part of a Group. So we have a 1 - many between group and users ( both are aggregates ).

Now, a user should know about it's users , so it contains a list with user ids ( I'm not putting a list of Users in their by purpose ).

But on the other hand, a User should also know in which group he is, so he should have a group id.

The problem here is that we have a bi directional association.

One way I can think to solve this is by not having a group id on the user, but retrieving the group containing the user id.

First of all is this an acceptable solution, if not, could you help me out working away that bi directional relationship ?

Thank you :)

  • Did you look into this article refactoring.guru/…? – Sazzad Hissain Khan Aug 28 '19 at 0:57
  • What's the use case of the user knowing which group he belongs to? Is this only for display purposes? – Subhash Bhushan Aug 28 '19 at 3:08
  • The term "Knows about" appear in books, its a nice word, but it is not always useful to think of relationships in this way. If you are designing a database, to keep your sanity, think in terms of the ER Modeling - Only! From ER view, a UserGroup Table would not have a userID FK. A User Table must have a UserGroupID FK. – NoChance Aug 28 '19 at 5:30
  • You may like to re-word "Now, a user should know about it's users ..." – NoChance Aug 28 '19 at 5:33
  • @SazzadHissainKhan yes I did, it's not really in the literal way "how to split a bidirectional", but more how to do it correctly, conceptually :) – TanguyB Aug 28 '19 at 6:59
3

Your initial approach is already leading you down a rabbit hole. You are trying to design a data-model instead of an object-model. The difference is the data-model models data and the object-model models behavior.

There is no "use-case" that a user needs to be a part of a group. The "use-case" is something the end-user does, like printing all groups on a web-ui, listing all users in a group on the web-ui, etc. Those are use-cases.

So I suggest you list all the behavior you need, try to assign them to either a User or a Group. After that you can add the data necessary for those behaviors to the objects.

  • Hi ! Thanks for the answer, after some reflection, my behavior will be around the group ( adding users, removing users, merging 2 groups ), the only time my user needs to know about it's group is when I want to display info of that group, that's not really behavior, so my behavior is located in my Group AR. – TanguyB Aug 29 '19 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.