0

I need to implement a generic repository (reuseable .dll) for applying operations ( e.g update, insert, delete, search ) keeping the fact we can make complex queries from database containing multiple joins and complexity.

I am doing in this way

public class GenericRepository<TEntity> where TEntity : class
{
//internal SchoolContext context; which context I need to use to behav as generic(.dll)
internal DbSet<TEntity> dbSet;

   public GenericRepository(SchoolContext context) //that won't be this context
   {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
   }
   .... public virtual void Insert(TEntity entity){..}
   .... public virtual void Delete(TEntity entityToDelete){..}
   .... public virtual void Update(TEntity entityToUpdate){..}
 }

So, which context, somehow to achieve the same working of DBContexts (i.e to interact with DB from objects using LINQ)?

Secondly, I am using some specific classes ( in case when this generic repository don't fulfills my operational requirement ) like some huge complex query. like

  class StudentRepository : GenericRepository
  {

  }
  class PersonRepository : GenericRepository
  {

  }

... Is this way is right or some other good implementation is available ?

Regards Usman

tereško
  • 58,060
  • 25
  • 98
  • 150
Usman
  • 2,742
  • 4
  • 44
  • 82

1 Answers1

2

Using specific repository is correct way because it encapsulate specific data access logic for given entity type / aggregate root. Using generic repository itself is not correct way because it doesn't bring any additional value. It is just wrapper for EF DbSet which already is "generic repository" and it still have all problems related to IDbSet / DbSet. If your main target is to make your code testable you will not achieve that with generic repository exposing IQueryable (to allow complex queries and joins not hidden by the repository).

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670