Following Repository Pattern properly I am new to learning Repository Pattern. I am developing a site in MVC 4 (I'm new to it as well).

The way I am using Repository pattern as follows:


I created IRepository&lt;T&gt; interface as follows:

public interface IRepository&lt;T&gt;    
{        
    void Insert(T entity);
    void Delete(T entity);
    IQueryable&lt;T&gt; SearchFor(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
    IQueryable&lt;T&gt; GetAll();
    T GetById(int id);
}

Then I created a base class which implements the above interface as follows:

public class BaseRepository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : class
{
    private readonly DbContext _dbContext;

    public BaseRepository(DbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public void Insert(TEntity entity)
    {
        _dbContext.Set&lt;TEntity&gt;().Add(entity);
        _dbContext.SaveChanges();
    }

    public void Delete(TEntity entity)
    {
        _dbContext.Set&lt;TEntity&gt;().Remove(entity);
        _dbContext.SaveChanges();
    }

    public IQueryable&lt;TEntity&gt; SearchFor(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
    {
        return _dbContext.Set&lt;TEntity&gt;().Where(predicate);
    }

    public IQueryable&lt;TEntity&gt; GetAll()
    {
        return _dbContext.Set&lt;TEntity&gt;();
    }

    public TEntity GetById(int id)
    {
        return _dbContext.Set&lt;TEntity&gt;().Find(id);
    }
}

Now for each repository I want to create, I create another interface which contains methods specific to that class. E.g if I want to create a CustomersRepository, I create an interface ICustomersRepository as follows:

public interface ICustomersRepository: IRepository&lt;Customer&gt;
{
    IEnumerable&lt;Customer&gt; GetRecentCustomers();
    Customer GetByName(string customerName);
}

Now I create CustomersRepository class, derive it from BaseRepository and  implement ICustomersRepository. as follows:

public class CustomersRepository : BaseRepository&lt;Customer&gt;, ICustomersRepository
{
    private readonly DbContext _dbContext;

    public CustomersRepository(DbContext dbContext) : base(dbContext)
    {
        _dbContext = dbContext;
    }

    public IEnumerable&lt;Customer&gt; GetRecentCustomers()
    {
        // implementation
    }


    public Customer GetByName(string customerName)
    {
        //
    }
}



Now to create another Repository, I will have to create an interface for it, then create that repository class which will implement BaseRepository&lt;TEntity&gt; class, implement the specific repository interface.

Issues:


This is not the proper way. But I use it as I get a BaseRepository&lt;TEntity&gt; which gives me many functions implemented.
I have to pass DbContext for each repository class.
not satisfied


Can you please guide me have this code corrected?  I will be using the same approach in almost all of my future projects where I use an ORM.
You can make DbContext protected property in your base implementation, so you don't have to declare it in every single derived class.
You might want to consider extracting Insert and Delete methods to separate interface, because often you want to make some of your repositories readonly.

Implementation may look like that:

interface IRepository&lt;T&gt;    
{        
    IQueryable&lt;T&gt; SearchFor(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
    IQueryable&lt;T&gt; GetAll();
    T GetById(int id);
}

interface IEditableRepository&lt;T&gt; : IRepository&lt;T&gt;
{
    void Insert(T entity);
    void Delete(T entity);
}

class ReadOnlyRepositoryBase&lt;T&gt; : IRepository&lt;T&gt;
{...}

class EditableRepositoryBase&lt;T&gt; : ReadOnlyRepositoryBase&lt;T&gt;, IEditableRepository&lt;T&gt;
{...}


this way you can distinct readonly repositories from non-readonly at compilation time.   
Passing dbContext to every repository is not normally an issue, because usually in addition to your repositories, you also implement factory object, which creates your repositories for you, so you don't have to call constructor explicitly. This factory can be made a singleton, so you can access it at any point in your code. Alternatively, you can register your repositories in some container, or you can use both approaches. 
If you are familiar with inversion of control and dependency injection (because there is definitely a learning curve), your best bet is to use a container.

Here is an example (using Castle Windsor):

public class RepositoryFactory : IDisposable
{
    private IWindsorContainer _container;

    public void Open()
    {
        _container = new WindsorContainer();

        //register dependencies
        _container.Register(Component.For&lt;DbContext&gt;().ImplementedBy&lt;DbContext&gt;());

        //register repositories
        _container.Register(Component.For&lt;ICustomersRepository&gt;().ImplementedBy&lt;CustomersRepository&gt;());
    }

    public TRepository GetRepository&lt;TRepository&gt;() where TRepository : class
    {
        return _container.Resolve&lt;TRepository&gt;();
    }

    public Dispose()
    {
        if (_container == null) return;
        _container.Dispose();
        _container = null;
    }

    public void Close()
    {
        Dispose();
    }
}


Then you can use it like this:

var f = new RepositoryFactory();
f.Open();
var rep = f.GetRepository&lt;ICustomersRepository&gt;();
f.Close();


Alternatively, you can implement your own container using reflection to instantiate your repositories, or you can use a dictionary. Or w/e, really. This implementation is obviously no singleton, but nothing stops you making one, if you feel like it.


Apart from that, your implementation looks fine to me. I didn't quite get your first issue though.