Problem of the concrete kind
I've an object A (an entity) that manipulates some companies (entity), companies can be linked together (if they have some common shareholder for example). I want A to be able to know wether or not a company C1 is linked with a company C2.
Feelings
Within my state of knowledge and habits, I think that there should be a method in the company Entity to tell wether or not it's connected with another one (point 1). Of courses I can do it by getting all the company connexions, and looking wether or not my company C2 is in there (point 2). But this is dirty, it means getting all dependencies for what reason ? Getting a boolean that could be retrieved easily from the database, I could think about making a function in the repository linked with the companies (point 3). But no, because nothing is available in the entities (either A or C1 and C2), light weight objects remember.
Argumentations
- Point 1 is based on the Single Responsibility Principle. Feel free to criticize that point. Since entity are supposed to embed the model logic, I think the isConnectedWith function should be right in the definition of the CompanyEntity. Please remember here that A is also an entity (so repository here too)
- Point 2 is a solution, it's good in one sense : it's the right way to do, if, that's a big if, what Doctrine is only simulating was true; if simply calling
$object->connexions
accessed to some objects collection that was already there, somewhere in the memory. But the truth is awful : it's not there, it lived the common bottleneck of our webapp : database. - Point 3 I'm getting sick of that Data Mapper & Dependency Injection combo telling that your entities (and a bit repositories) should live only by themselves because they are the model logic. Maybe I'm terribly wrong on my understanding, but since everybody is telling it's a bad thing, I've completely dropped the idea of injecting helper or configuration in the entities.
Questions
How would you solved the problem maintaining integrity and performances and avoiding dirty workaround ? What points of my argumentations are bad ?