I've been struggling with this question for a while.
I'm specifically thinking within the object orientated domain model.
Let's say I have two entities. A supplier
and a customer
. There is a relationship that a supplier
can have many customers, and a customer
(in this case) can only have one supplier
.
The issue I have is that neither object needs any behaviour of the other object i.e. the supplier object does not need to use any behaviour of the customer object and visa versa.
There is however still an association between them. The reason is that the problem domain requires that all a suppliers customers are destroyed if the supplier is destroyed. We also need to be able to retrieve a collection of all customers associated with a supplier.
How is this managed in the OO domain model? Who is responsible for the association?
Some may say that there should be a getCustomers()
method on the supplier
object and this is where the association lies. I don't really like this as it makes it difficult to close the supplier
class (violation of Open Closed Principle). Every time we create a new type of class which has an association to a supplier
, we have to add a get method to the supplier
.
One thing I think could be used to model the association are repositories (from Eric Evens Domain Driven Design book). I guess it would be the Repository that then cares about this relationship? We could call selectCustomersFor(supplier_id)
etc
Edit
The only reason I can see for having the association is for object retrieval criteria or object destruction (i.e. if user is destroyed, all customers will be destroyed). To me, it is not the responsibility of either object to retrieve or destroy itself. You could argue that the supplier should retrieve/destroy a customer, but this surely violates the Single Responsibility rule, and could also lead to Open Closed violation.
Why would we then bother to add the responsibility of association to an object thats doesn't use it? Its useless to the object. The association isn't useless though, I just feel something else should be responsible for it.