简体   繁体   中英

MVC Generic Repository/Unit of Work pattern - Extending a repository

Say I have the following implementation:

//Generic repository.
public interface IRepository<T> where T : class {
   void Insert(T entity);
   void Delete(int id);
   void SaveChanges();
   //..more generic functions
}

//Repository implementation.
public class EFRepository<T> : IRepository<T> where T: class {
   private MyDbContext context;
   protected DbSet<T> dbSet;

   public EFRepository(): this(new MyDbContext()){}

   public EFRepository(MyDbContext context)
   {
      this.context = context;
      dbSet = context.Set<T>();
   }

   public void Insert(T entity)
   {
      dbSet.Add(entity);
   }

   public void Delete(int id)
   {
      dbSet.Remove(dbSet.Find(id));
   }

   public void SaveChanges()
   {
      context.SaveChanges();
   }
   //...more generic implementations
}

//Unit of Work Interface
public interface IUnitOfWork: IDisposable
{
   IRepository<EntityA> ARepository { get; }
   IRepository<EntityB> BRepository { get; }
   //...more stuff
}

//Unit of Work Implementation
public class EFUnitOfWork: IUnitOfWork
{
   private MyDbContext context = new MyDbContext();

   private IRepository<EntityA> aRepository;
   private IRepository<EntityB> bRepository;

   public IRepository<EntityA> ARepository 
   {
      get
      {
            if (this.aRepository == null)
               this.aRepository = new EFRepository<EntityA>(context);

            return this.aRepository;
      }

   }

   public IRepository<EntityB> BRepository 
   {
      get
      {
            if (this.bRepository == null)
               this.bRepository = new EFRepository<EntityB>(context);

            return this.bRepository;
      }

   }

   //...more stuff
}

And finally, I have the following bindings in my resolver:

kernel.Bind(typeof(IRepository<>)).To(typeof(EFRepository<>));
kernel.Bind(typeof(IUnitOfWork)).To(typeof(EFUnitOfWork));

Now, my question is... how would I go about extending the repository for EntityA so that it has more operations than just the generic ones?

I'll post what I have so far in a few...

EDIT: Here's what I have so far:

//New interface.
public class IEntityARepository : IRepository<EntityA>
{
   void DoSomethingSpecificToEntityA();
}

//New implementation.
public class EFEntityARepository : EFRepository<EntityA>
{
   public EFEntityARepository(MyDbContext context) : base(context) {}
   //add additional methods for EntityA
   public void DoSomethingSpecificToEntityA()
   {

   }

}


//Modify Unit of Work Interface as follows.
//Unit of Work Interface
public interface IUnitOfWork: IDisposable
{
   IEntityARepository  ARepository { get; }
   IRepository<EntityB> BRepository { get; }
   //...more stuff
}

//Modify Unit of Work Implementation as follows.
public class EFUnitOfWork: IUnitOfWork
{
   private MyDbContext context = new MyDbContext();

   private IEntityARepository  aRepository;
   private IRepository<EntityB> bRepository;

   public IEntityARepository   ARepository 
   {
      get
      {
            if (this.aRepository == null)
               this.aRepository = new EFEntityARepository<EntityA>(context);

            return this.aRepository;
      }

   }

   public IRepository<EntityB> BRepository 
   {
      get
      {
            if (this.bRepository == null)
               this.bRepository = new EFRepository<EntityB>(context);

            return this.bRepository;
      }

   }

   //...more stuff
}

Add the following binding:

kernel.Bind(typeof(IEntityARepository)).To(typeof(EFEntityARepository));

However, I'm sure this is not correct. Or at least, not the proper way to do it.

If I understand you correctly, you could just derrive from a specifically typed version of your generic class like this...

public class EFEntityARepository : EFRepository<EntityA>, IEntityARepository 
{
    //Add more opps
}

I think the unit of work needs to look like this:

   public IEntityARepository   ARepository 
   {
      get
      {
            if (this.aRepository == null)
               this.aRepository = new EFEntityARepository(context);

            return this.aRepository;
      }

   }

OK so I've got it working by adding and/or modifying my original code as follows:

//New interface for the extension of the repository.
//Is it possible to do this without defining this new interface? Doesn't seem like it.
public class IEntityARepository : IRepository<EntityA>
{
   void DoSomethingSpecificToEntityA();
}

//Add new class.
//It looks like you have to inherit from IEntityARepository as well.
public class EFEntityARepository : EFRepository<EntityA>, IEntityARepository
{

   public EFEntityARepository(MyDbContext context) : base(context) {}

   //add additional methods for EntityA
   public void DoSomethingSpecificToEntityA()
   {

   }

}


//Modify Unit of Work Interface as follows.
public interface IUnitOfWork: IDisposable
{
   IEntityARepository ARepository { get; }
   IRepository<EntityB> BRepository { get; }
   //...more stuff
}

//Modify Unit of Work Implementation as follows.
public class EFUnitOfWork: IUnitOfWork
{
   private MyDbContext context = new MyDbContext();

   private IEntityARepository aRepository;
   private IRepository<EntityB> bRepository;

   public IEntityARepository ARepository 
   {
      get
      {
            if (this.aRepository == null)
               this.aRepository = new EFEntityARepository(context);

            return this.aRepository;
      }

   }

   public IRepository<EntityB> BRepository 
   {
      get
      {
            if (this.bRepository == null)
               this.bRepository = new EFRepository<EntityB>(context);

            return this.bRepository;
      }

   }

   //...more stuff
}

It works... but is it the best way to go about it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM