views:

65

answers:

1

Should a collection of objects also be responsible for creating new objects?

Let me explain. I have two classes, PhoneCall and PhoneCallList. PhoneCallList is a collection of PhoneCall objects. Our architect and I are at odds on how to go about creating new PhoneCall objects. Currently the PhoneCall object has a method called Create that adds the record to the database and the PhoneCallList object has a method called Add which adds a PhoneCall to the list.

The architect wants to have the Add method in the PhoneCallList also create the new record in the database and add it to the collection. Is doing it this way violating the Single Responsiblility Principle? To me it feels like this is not the best way to go about this.

Which is the best way to do it?

*Neither object has knowledge of the DB. The call service that takes care of all of the data access. The question is should the PhoneCall or PhoneCallList object be calling the service that does the data access?

+1  A: 

There is much I'm uncertain of regarding context but it looks like two issues. Who should create a PhoneCall object and who should add a PhoneCall to a database. I would suggest that PhoneCall objects be created by whatever higher up module is manipulating the PhoneCallList and pass that to an 'Add' function of the PhoneCallList. I would also lean towards neither class doing the DB entry. That sounds like a good reason for a 3rd class.

Arnold Spence
+1, why would either of the two classes have knowledge of the db. In fact that would violate the single resposibility principle
TT
I updated the question with regards to knowledge of the DB.
I'm glad to hear they have no knowledge of the DB, but they shouldn't have knowledge of the service that manages the DB either. 'Something' should pass a PhoneCall object to the PhoneCallList and 'Something' should pass the service whatever it needs too.
Arnold Spence