The repository pattern is used in the context of an MVC web application framework to decouple the particular mechanism of storage retrieval from the Controller.
Whenever I've ended up using this pattern, there ends up being an explosion of methods in the repository class per unique query. For example, in the article I linked earlier there are 7:
- Grab all questions asked
- Create a bare question for form binding in our views
- Grab paginated questions
- Create and store a question
- Grab a question by its primary key
- Update a question with a primary key and some array data
- Delete a question by its primary key
I can imagine more:
- Grab all questions with an answer
- Grab all questions without an answer over a certain age
- Grab all questions with certain key words
- Grab all questions marked as spam or deleted
- Grab all users that asked a question
Basically it seems under this pattern there will be one method per unique set of where clause conditions, such that the methods are isomorphic to unique queries. Is this a bad thing? Is there a more general pattern to be applied to prevent this explosion of methods?
(12) presents a related problem - is a Question repository supposed to leak the details of the relationships that Questions have to Users?