1

I use EF in my project and I want to implement the repository design pattern, but I want to have one super class which will take care for the Insert / Update / Delete / SaveChanges methods in order to avoid code duplication.

For example, I have these two interfaces ICostTypeRepository and IEmailRepository:

public interface ICostTypeRepository : IDisposable
{
        CostType GetCostTypeById(int pId);
        CostType GetCostTypeByCode(string pCode);
        IEnumerable<CostType> GetCostTypes();

        void Insert(CostType pCostType);
        void Update(CostType pCostType);
        void Delete(CostType pCostType);
        void SaveChanges();
}

public interface IEmailRepository : IDisposable
{
    Email GetEmailByID(int pId);
    IEnumerable<Email> GetEmails();
    Email GetEmailByAdress(string pAddress);

    void Insert(Email pEmail);
    void Update(Email pEmail);
    void Delete(Email pEmail);
    void SaveChanges();
}

What is the best way of creating an interface (and its implementation in a class) let's call it IDbOperations which will contain the CRUD methods only?

Something like

public interface IDbOperations : IDisposable
{     
    void Insert(??);
    void Update(??);
    void Delete(??);
    void SaveChanges();
}

What parameters will these CRUD methods take?

4
  • learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/… Commented Sep 16, 2017 at 21:37
  • You'd most likely have to make it a generic interface - IDbOperations<T>, and T would be the type of class you're using, e.g. IDbOperations<Email> would then have methods like Insert(Email email) etc. Commented Sep 16, 2017 at 21:37
  • Why do you need the repository pattern? Entity Framework DbContextes are already a repository pattern abstraction of sorts. Commented Sep 16, 2017 at 23:37
  • DbContext already has Insert, Update Delete and SaveChanges for any entity, why do you think you need to add another abstraction on top of this? Commented Sep 16, 2017 at 23:40

1 Answer 1

3

Vane,

What you need is the generic repository pattern. You have to code the basic operations in a base class and then make extend all the repository classes from it. Inside each new repository class you can create more specific methods.

This is an example of a Generic Repository interface

public interface IGenericRepository<T> where T : class {

IQueryable<T> GetAll();
IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Delete(T entity);
void Edit(T entity);
void Save();
}

You can implement the generic repository like this:

public class GenericRepository<C, T> : 
IGenericRepository<T> where T : class where C : DbContext, new() {

private C _entities = new C();
public C Context {

    get { return _entities; }
    set { _entities = value; }
}

public virtual IQueryable<T> GetAll() {

    IQueryable<T> query = _entities.Set<T>();
    return query;
}

public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate) {

    IQueryable<T> query = _entities.Set<T>().Where(predicate);
    return query;
}

public virtual void Add(T entity) {
    _entities.Set<T>().Add(entity);
}

public virtual void Delete(T entity) {
    _entities.Set<T>().Remove(entity);
}

public virtual void Edit(T entity) {
    _entities.Entry(entity).State = System.Data.EntityState.Modified;
}

public virtual void Save() {
    _entities.SaveChanges();
}
}

Now that you have the GenericRepository class, make FooRepository extend it and add specific methods (if needed) like this:

public class FooRepository :
GenericRepository<FooBarEntities, Foo>, IFooRepository {

public Foo GetSingle(int fooId) {

    var query = GetAll().FirstOrDefault(x => x.FooId == fooId);
    return query;
}
}

You should read about Unit of work pattern. It makes a lot of sense with the repository pattern. Please read this article from Microsoft.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.