Skip to content

Entity contain another entity #111

Closed Answered by Sairyss
yookky asked this question in Q&A
Discussion options

You must be logged in to vote

Your example is partially correct.

  1. If your entity contains other entities, it is called an Aggregate Root, and all manipulations with child entities must be performed through the aggregate root. In your example you have a get() method that returns a child entity, but it makes it possible to violate the rule like this:
const userBook = userEntity.get(); // <-- returns UserBookEntity
userBook.doSomething() // <-- aggregate integrity violated

You should not return child entities from an Aggregate Root (you can return it's data though if you need to, but it must be immutable).

If you need to perform manipulations to child entities, do this instead:

userEntity.doSomethingToBook(); // <-- met…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@yookky
Comment options

Answer selected by yookky
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants