1

说我有下面的实现:MVC通用存储库工作模式/股 - 扩展的存储库

//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)); 

但是,我确定这是不正确的。或者至少,不是正确的做法。

回答

3

如果我理解正确的话,你可以只从你喜欢这个泛型类的具体类型版本derrive ...

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

    } 
+0

就是这样吗?我是否也不需要对工作单元进行更改? – Seriphos

+0

查看上面的编辑,我想你只需要返回派生类的一个实例。 –

+0

如果您看到我的原始帖子的编辑,我的新实现已经有了。但似乎我已经添加/修改了比我需要的更多东西。 – Seriphos

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 
} 

它的工作原理...但它是最好的方式去做呢?

+0

这个评论对派对来说真的很晚,但我跟着你的解决方案来处理我遇到的同样的问题,并发现它的工作原理。我也认为可能有不同的方式去做。自上次发布后,您是否找到了更好的实施方案? –