Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository Pattern with Stored Procedures

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.

like image 288
jodev Avatar asked Aug 04 '11 07:08

jodev


People also ask

Should I use repository pattern with dapper?

Large-scale, complex projects benefit the most from readability improvments, and when using Dapper and C# the Dapper Base Repository pattern allows for better code readability and a single point of failure by creating a base class with generic methods that allow for querying and execution against a SQL database.

When should we use repository pattern?

The Repository pattern makes it easier to test your application logic. The Repository pattern allows you to easily test your application with unit tests. Remember that unit tests only test your code, not infrastructure, so the repository abstractions make it easier to achieve that goal.

Do we need repository pattern with Entity Framework?

The single best reason to not use the repository pattern with Entity Framework? Entity Framework already implements a repository pattern. DbContext is your UoW (Unit of Work) and each DbSet is the repository. Implementing another layer on top of this is not only redundant, but makes maintenance harder.

What is ORM Dapper with repository pattern?

What is Dapper? It is an ORM [Object Relational Mapper] which basically is an open source and light-weight ORM for developers who prefer to work with ADO.NET technology. It is in top most ORMs which ensure the high performance. It works with both, static and dynamic, objects.


2 Answers

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.

like image 144
Mark Watts Avatar answered Oct 05 '22 12:10

Mark Watts


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);
        }
like image 42
rsuharta Avatar answered Oct 05 '22 11:10

rsuharta