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.
can't you added a new method in your IRepository to execute custom stored proc:
/// <summary>
/// Execute Stored Proc with result set returned.
/// </summary>
/// <param name="procName"></param>
/// <returns>An object resultset</returns>
T ExecuteCustomStoredProc<T>(string procName, SqlParameter params);
and in your Implementation (ConcreteRepository) you can put this logic it:
public T ExecuteCustomStoredProc<T>(string commandName, SqlParameter params)
{
return this._repositoryContext.ObjectContext.ExecuteStoreQuery<T>(commandName, params);
}
I'd suggest that if you are using GetAll with a stored procedure you are kind of missing the point.
The example of GetAll that returns an IQueryable infers delay execution in some form, but if you are making a call to a stored procedure the execution will not be delayed.
I'd suggest keeping the GetAll function, but as a call into your ORM's context. The calls you have to the stored procedures, keep as separate methods, but return something like IList<Entity>
精彩评论