Repository pattern for books in EF6 I am working on a generic repository. And I wondered if anyone could give some feedback on it.

Interface for DbContext:

public interface IDbContext
{
    IDbSet&lt;T&gt; Set&lt;T&gt;() where T : class;
    DbEntityEntry Entry&lt;T&gt;(T entity) where T : class;
    void SaveChanges();
}


Context class

public class BookContext : DbContext, IDbContext
{
    public BookContext()
        : base("BookDB")
    { }

    public new IDbSet&lt;T&gt; Set&lt;T&gt;() where T : class
    {
        return base.Set&lt;T&gt;();
    }

    public new DbEntityEntry Entry&lt;T&gt;(T entity) where T : class
    {
        return base.Entry(entity);
    }

    public new void SaveChanges()
    {
        base.SaveChanges();
    }
}


Repository interface

public interface IRepository&lt;T&gt;
{
    IQueryable&lt;T&gt; FindAll();
    IQueryable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
    T FindById(int id);
    void AddOrUpdate(T entity);
    void Remove(T entity);
}


Repository implementation

public class Repository&lt;T&gt; : IRepository&lt;T&gt;
    where T : class, IEntity
{
    protected readonly IDbContext Context;

    public Repository(IDbContext context)
    {
        Context = context;
    }

    public IQueryable&lt;T&gt; FindAll()
    {
        return Context.Set&lt;T&gt;();
    }

    public IQueryable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
    {
        return Context.Set&lt;T&gt;().Where(predicate);
    }

    public T FindById(int id)
    {
        return Context.Set&lt;T&gt;().SingleOrDefault(o =&gt; o.Id == id);
    }

    public void AddOrUpdate(T entity)
    {
        if (entity == null)
            throw new ArgumentNullException("entity");

        if (entity.Id == 0)
        {
            Context.Set&lt;T&gt;().Add(entity);
        }
        else
        {
            Context.Entry(entity).State = EntityState.Modified;
        }

        Context.SaveChanges();
    }

    public void Remove(T entity)
    {
        if (entity == null)
            throw new ArgumentNullException("entity");

        Context.Set&lt;T&gt;().Remove(entity);
        Context.SaveChanges();
    }
}


Additional repository

public interface IBookRepository
{
    Book FindByIsbn(string isbn);
    IQueryable&lt;Book&gt; FindByAuthor(Author author);
}


Book repo implementaion

public class BookRepository : Repository&lt;Book&gt;, IBookRepository
{
    public BookRepository(IDbContext context) : base(context)
    {
    }

    public Book FindByIsbn(string isbn)
    {
        return Context.Set&lt;Book&gt;().SingleOrDefault(b =&gt; b.Isbn.Equals(isbn));
    }

    public IQueryable&lt;Book&gt; FindByAuthor(Author author)
    {
        return Context.Set&lt;Book&gt;().Where(b =&gt; b.Author.Id == author.Id);
    }
}


I am planning to use this repo with autofac IoC. I am not sure If I should use Unit of work pattern and should I make my repos disposable. Any feedback would be appreciated.
1) I would not expose IQueryable via IRepository. IQueryable is still a virtual set of objects that hasn't been retrieved from a database yet. If you have (for example) some problem with database performance, you would have to go through all your application to find out what queries you construct and send to DB.

Instead I would expose methods for specific purposes on specific repositories. These methods would return IList&lt;T&gt; (or ICollection&lt;T&gt;, depends on your preference). Then you have all the code for accessing a database on a single place. For example, your IBookRepository would look like:

public interface IBookRepository : IRepository&lt;Book&gt;
{
    Book FindByIsbn(string isbn);
    IList&lt;Book&gt; FindByAuthor(int authorId);
    IList&lt;Book&gt; FindByTitle(string title);
    ...
}


And your generic repository would be:

public interface IRepository&lt;T&gt;
{
    T FindById(int id);
    void AddOrUpdate(T entity);
    void Remove(T entity);
}


2) It is not necessary to make your repositories disposable. So far you have nothing to dispose there. Your repositories get IDbContext (which is IDisposable) from outside, thus it should not be disposed inside a repository. If your repositories get created via an IoC container, the container will also handle disposing of all dependencies.

3) UnitOfWork: You can implement UnitOfWork pattern if you want to (e.g.) wrap all your database work inside transactions. I would recommend such IUnitOfWork interface:

public interface IUnitOfWork
{
    void SaveChanges();
    void BeginTransaction();
    void CommitTransaction();
    void RollbackTransaction();
}


Implemetation of IUnitOfWork would have dependency on IDbContext. Then the only places where you work with a database (via IDbContext) are UnitOfWork and Repository classes. At places where you want to save your work to a DB, use IUnitOfWork, not IDbContext.