So, do you even model the relationship in the parent class in that case, i.e. with a private data member of type List<Car>, for example? Would your CarCompany look like:
or would it look like this:Code:public class CarCompany { private List<Car> cars; private CarLocator carLocator; public List<Car> getCars() { if (cars == null) cars = carLocator.getCars(); return this.cars; } }
The problem with the first option is that it seems redundant - should you handle relationship caching in your domain model? The second option, though, looks like an anemic model from the inside. From the outside, you still see the same behavior, namely CarCompany.getCars(), for example, but the inside is barren.Code:public class CarCompany { private CarLocator carLocator; public List<Car> getCars() { return carLocator.getCars(); } }
If you went to an ORM solution like Hibernate, I would think that you'd want the first option, so that Hibernate could manage that parent-child relationship. You would also have the option of dumping the CarLocator entirely and have Hibernate lazy-load the cars child relationship.
Where do you draw the line with Hibernate regarding injecting locators and letting Hibernate manage the parent-child relationships? Would you maybe have a CarCompany that looks like this:
In this case, you'd have Hibernate manage the factories relationship and then use the DAO for the cars one.Code:public class CarCompany { private List<Car> cars; private List<Factory> factories; private CarLocator carLocator; public List<Car> getCars() { if (cars == null) cars = carLocator.getCars(); return this.cars; } public List<Factory> getFactories() { return this.factories; } }
The problem is that I've got a richly-connected domain, and I'm trying to decide where you draw the line between populating the parent-child relationships at object instantiation, whether in the data access object or in the ORM persistence layer, or using locator or DAO services to load those relationships on-demand.
Thoughts? Anyone else encounter this kind of situation?


Reply With Quote

