1

假设我有以下实现:

//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
}

最后,我的解析器中有以下绑定:

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

现在,我的问题是......我将如何扩展 EntityA 的存储库,以便它具有比通用操作更多的操作?

我会在几篇文章中发布我到目前为止所拥有的...

编辑:这是我到目前为止所拥有的:

//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
}

添加以下绑定:

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

但是,我确信这是不正确的。或者至少,不是正确的方法。

4

2 回答 2

3

如果我对您的理解正确,您可以像这样从通用类的特定类型版本中派生出...

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

我认为工作单元需要如下所示:

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

            return this.aRepository;
      }

   }
于 2012-12-19T16:38:30.923 回答
1

好的,我已经通过添加和/或修改我的原始代码来使其工作,如下所示:

//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
}

它有效......但这是最好的方法吗?

于 2012-12-19T17:44:52.697 回答