Reputation: 379
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:
I can imagine more:
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?
Upvotes: 2
Views: 567
Reputation: 2954
here you would find your solutions :
eloquent tricks better repositories
creating flexible controllers in laravel 4 using repositories
Upvotes: 0
Reputation: 3664
What all queries have in common ? Well... they're all queries... :) They all accept some parameters and return some structure object.
You can create a generic abstract interface/class that makes use of that fact:
(C#)
public interface IQueryHandler<TQuery, TResult> where TQuery : IQuery<TResult>
{
TResult Handle(TQuery query);
}
It will not only help you with decoupling and abstracting all types of queries you mentioned, it is also very useful in Depedency Injection and applying cross-cutting concerns.
These two articles that taught me that explain it in detail:
It's in C#, but you can create a mirror implementation in every language that supports templates (c++, java etc.)
Upvotes: 1