好吧,让我分解一下我一直在尝试做的事情:
首先是 abstract
我的通用存储库:
public abstract class Repository<T, C> where T : class where C : DbContext, new() {
private C _entities = new C();
public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate) {
IQueryable<T> query = _entities.Set<T>().Where(predicate);
return query;
}
public void Add(T entity) {
_entities.Set<T>().Add(entity);
}
public void Delete(T entity) {
_entities.Set<T>().Remove(entity);
}
public void Edit(T entity) {
_entities.Entry(entity).State = System.Data.EntityState.Modified;
}
public void Save() {
_entities.SaveChanges();
}
}
另外,这是一个我将用于我的 AccommPropertyWebDetailRepository
的界面存储库class
:
public interface IAccommPropertyWebDetailRepository {
IQueryable<AccommPropertyWebDetail> GetAll(ApprovalStatus approvalstatus = ApprovalStatus.All);
AccommPropertyWebDetail GetSingle(int accommPropertyWebDetailId, ApprovalStatus approvalstatus = ApprovalStatus.All);
AccommPropertyWebDetail GetSingleByAccommPropertyId(int accommPropertyId, ApprovalStatus approvalstatus = ApprovalStatus.All);
}
下面是我的AccommPropertyWebDetailRepository
类:
public class AccommPropertyWebDetailRepository : Repository<AccommPropertyWebDetail, ReservationHubEntities>, IAccommPropertyWebDetailRepository {
ReservationHubEntities _entities = new ReservationHubEntities();
public IQueryable<AccommPropertyWebDetail> GetAll(ApprovalStatus approvalstatus = ApprovalStatus.All) {
IQueryable<AccommPropertyWebDetail> query = _entities.AccommPropertyWebDetails;
switch (approvalstatus) {
case ApprovalStatus.Approved:
query = query.Where(x => (x.AccommProperty.IsApproved == true) && (x.AccommProperty.IsLockedForView == false));
break;
case ApprovalStatus.NotApproved:
query = query.Where(x => (x.AccommProperty.IsApproved == false) || (x.AccommProperty.IsLockedForView == true));
break;
}
return query;
}
public AccommPropertyWebDetail GetSingle(int accommPropertyWebDetailId, ApprovalStatus approvalstatus = ApprovalStatus.All) {
var query = GetAll(approvalstatus).First(x => x.AccommPropertyWebDetailID == accommPropertyWebDetailId);
return query;
}
public AccommPropertyWebDetail GetSingleByAccommPropertyId(int accommPropertyId, ApprovalStatus approvalstatus = ApprovalStatus.All) {
var query = GetAll(approvalstatus).Single(x => x.AccommPropertyID == accommPropertyId);
return query;
}
}
所以到目前为止一切都很好(根据我的说法,但我不确定我错过了什么)。
我遇到的真正问题是在 ASP.NET MVC Web 应用程序方面。
假设我的 Controller 类按如下方式启动:
public AccommPropertyController(
IAccommPropertyPictureRepository accommpropertypicturerepo) {
_accommpropertypicturerepo = accommpropertypicturerepo;
}
private readonly IAccommPropertyPictureRepository _accommpropertypicturerepo;
对于依赖注入(inject),我有以下代码(我使用 Ninject 进行依赖注入(inject)):
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel) {
kernel.Bind<IAccommPropertyPictureRepository>().
To<AccommPropertyPictureRepository>();
}
那么,Repository<T, C>
在哪里?抽象类应该适合这里吗?
因为,我没有直接使用AccommPropertyPictureRepository
在我的 Controller 内,仅使用 IAccommPropertyPictureRepository
界面,我的 Controller 对Repository<T, C>
一无所知抽象类。
有什么已知的方法可以处理这个恼人的问题吗?
更新1
所以,现在在@Darin的建议之后,我有以下interface
public interface IRepository<T, C> where T : class where C : DbContext {
IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Delete(T entity);
void Edit(T entity);
void Save();
}
我的抽象类如下:
public abstract class Repository<T, C> : IRepository<T, C> where T : class where C : DbContext, new() {
private C _entities = new C();
public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate) {
IQueryable<T> query = _entities.Set<T>().Where(predicate);
return query;
}
public void Add(T entity) {
_entities.Set<T>().Add(entity);
}
public void Delete(T entity) {
_entities.Set<T>().Remove(entity);
}
public void Edit(T entity) {
_entities.Entry(entity).State = System.Data.EntityState.Modified;
}
public void Save() {
_entities.SaveChanges();
}
}
无法弄清楚其余部分。
更新2
现在我也想通了。这是IAccommPropertyPictureRepository
接口(interface):
public interface IAccommPropertyPictureRepository<T, C> : IRepository<T, C> where T : class where C : DbContext {
IQueryable<AccommPropertyPicture> GetAll(ApprovalStatus approvalstatus = ApprovalStatus.All);
IQueryable<AccommPropertyPicture> GetAll(int accommPropertyId, ApprovalStatus approvalstatus = ApprovalStatus.All);
AccommPropertyPicture GetSingle(int accommPropertyPictureId, ApprovalStatus approvalstatus = ApprovalStatus.All);
AccommPropertyPicture GetSingle(Guid accommPropertyPictureGUID, ApprovalStatus approvalstatus = ApprovalStatus.All);
}
这是AccommPropertyPictureRepository
类:
public class AccommPropertyPictureRepository : Repository<AccommPropertyPicture, ReservationHubEntities>, IAccommPropertyPictureRepository<AccommPropertyPicture, ReservationHubEntities> {
ReservationHubEntities _entities = new ReservationHubEntities();
public IQueryable<AccommPropertyPicture> GetAll(ApprovalStatus approvalstatus = ApprovalStatus.All) {
IQueryable<AccommPropertyPicture> query = _entities.AccommPropertyPictures;
switch (approvalstatus) {
case ApprovalStatus.Approved:
query = query.Where(x => (x.AccommProperty.IsApproved == true) && (x.AccommProperty.IsLockedForView == false));
break;
case ApprovalStatus.NotApproved:
query = query.Where(x => (x.AccommProperty.IsApproved == false) || (x.AccommProperty.IsLockedForView == true));
break;
}
return query;
}
public IQueryable<AccommPropertyPicture> GetAll(int accommPropertyId, ApprovalStatus approvalstatus = ApprovalStatus.All) {
var query = GetAll(approvalstatus).Where(x => x.AccommPropertyID == accommPropertyId);
return query;
}
public AccommPropertyPicture GetSingle(int accommPropertyPictureId, ApprovalStatus approvalstatus = ApprovalStatus.All) {
var query = GetAll(approvalstatus).First(x => x.AccommPropertyPictureID == accommPropertyPictureId);
return query;
}
public AccommPropertyPicture GetSingle(Guid accommPropertyPictureGUID, ApprovalStatus approvalstatus = ApprovalStatus.All) {
var query = GetAll(approvalstatus).First(x => x.AccommPropertyPictureGUID == accommPropertyPictureGUID);
return query;
}
}
我现在已经成功构建了。应该Ninject
东西保持不变?我想我只需要更改 Controller 构造函数中的一些代码,对吗?
最佳答案
一种可能是有一个基地 IRepository<T, C>
包含所有必要操作的接口(interface),将由 Repository<T, C>
实现抽象类以及IAccommPropertyWebDetailRepository
界面。
关于c# - EntityFramework 存储库从接口(interface)和抽象类 : how to use dependency inject on ASP. NET MVC 驱动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7612459/