In all the apps I've written, I've always put this sort of thing in my DAL, which uses the factory pattern. So ChildFactory.newChild(Parent p) would call child.setParent and p.getChildren().add(newChild). You could also put it in business logic, but that leaves you open to errors of omission if you have to do it in more than one place.
It is reasonable to not expose the Child.setParent() method, and to have Parent.getChildren() return an immutable Collection. Then you would have your factory access those methods through the implementation class instead of the interface. This guarantees reasonable protection.
There are any number of options to deal with this. I recommend reading up on patterns of use in data access applications, as my suggestion here may not be suitable for you, especially if you haven't separated your DAL and your business logic very cleanly.
|