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) etcEditThe 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.
Domain model associations that do not required the behaviour of the associated object. How to model?
object oriented;domain driven design;domain model;relationships
Q:Why objects reference each other?A:To make the model consistent and valid.Let's try to model your domain. Supplier is an aggregate. Customer is an aggregate. Suppliers are created and managed in their bounded context. In this context I can create/update/delete/enable/disable suppliers.Customers are managed in their bounded context. Any customer must be associated to a certain supplier. Why you ask? coz that's what the domain experts told us. So whenever a customer is created I'll pass it a supplierId value object.But Suppliers and Customers don't need anything from each other?Actually they do. Each of them need to be valid and consistent in the system. A Customer must be associated with a certain Supplier to be a valid customer.When you need to delete a Supplier you will delete it in its bounded context then send a domain event to the Customer's bounded context with the supplier's id. You will retrieve all suppliers associated to that supplier and delete them.Remember the one aggregate per transaction rule.In case you modeled everything as a single bounded context then in your application service start transaction, fetch the supplier from its repository, delete it, fetch the associated customers from their repository, delete them then commit the transaction.This might be quick and dirty, but it may suffice your needs.