I’m pretty new to the repository pattern and dependency injection. Almost all repository patterns I’ve come across has some sort of GetAll() method like so:
public interface IRepository<T>
{
IQueryable<T> GetAll();
// other CRUD methods here...
}
I’m having an issue implementing this interface and the GetAll() method because I am calling a stored procedure that requires a parameter that changes based on a user’s input. I don’t want to add an ad-hoc method to the repository interface e.g. IQueryable<T> GetAll(string input);. I also don’t want to add a parameter to the constructor because it looks a little messy to me:
public class ConcreteRepository : IRepository<Entity>
{
string _storedProcedureInput;
public ConcreteRepository(string storedProcedureInput)
{
_storedProcedureInput = storedProcedureInput;
public IQueryable<Entity> GetAll()
{
// Call to stored procedure goes here passing in the
// _storedProcedureInput variable.
}
}
I’m also using dependency injection so I would have to add some dynamic input to the constructor when binding:
Bind<IRepository<Entity>>().To<ConcreteRepository>().WithConstructorArgument(?)
Any suggestions?
UPDATE:
I’d like to reuse the IRepository interface. For example, in one program I’m using EF4 to implement the GetAll() method, and in another program I’m using standard ADO.NET to call a stored procedure like the example above.
It sounds like your GetAll isn’t necessarily getting all. In which case you may as well rename it or have another method which more accurately describes the functionality offered by your stored procedure which takes appropriate input parameter(s) that can be passed to the procedure.