I think this is also a matter of modeling. Itโs normal to think that the User has a Department and the Department has Users, but the question is how deep can you look at the data from the user and the department?
Does it make sense if conceptually you do not get access to user.department.user [2] .name? What about department.user [10] .addresses [1] .street?
I really don't think about it in most scenarios. This is an information domain question. You have bonds when accessing data, and it can also be somehow expressed in your models.
If the Object Modeling object represents the real world, itโs normal to think that when you go to the department, you will see that dozens of people work there, and most likely all you can find out about them is the count and their names, maybe . So, what pieces of data should you see from your object?
My approach for this is:
interface PersonInfo { String name(); String lastName(); default fullName() { return name() + " " + lastName(); } static PersonInfoBuilder personInfo() { return new PersonInfoBuilder(); } static class PersonInfoBuilder { ... } } interface Person extends PersonInfo { DepartmentInfo department(); Set<Address> addresses();
I donโt think that I will need to show how the builders will work, because if you noticed, for this scenario, the bidirectional nature of the relationship will never be. Therefore, when you create a Person, all you need is DepartmentInfo (the department does not need employees), and this is true when creating a department, when all you need is PersonInfo from the employees of the department.
This is my way of thinking about this problem conceptually. Any comments?
source share