c# - EntityFramework 存储库从接口(interface)和抽象类 : how to use dependency inject on ASP. NET MVC 驱动

标签 c# asp.net-mvc asp.net-mvc-3 dependency-injection ninject

好吧,让我分解一下我一直在尝试做的事情:

首先是 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/

相关文章:

asp.net-mvc-3 - 是否有可以嵌入 ASP.NET MVC3 应用程序的 Web 服务器

c# - string.Format "C"(货币)返回字符串 "C"而不是格式化文本

c# - 应用程序设置不会在 Xamarin.Forms 中删除

c# - 使用 jQuery 而不是 AJAX 来模拟 UpdatePanel 功能

c# - 更新 Azure Blob 上的 LastModified

asp.net-mvc - 如何在ASP.NET MVC部分 View 中将匿名列表用作模型?

c# - 如何在 ASP.NET MVC 中使用 JQuery.Ajax 传递多个参数

jquery - 如何从失败的 jQuery 请求中获取异常消息?

javascript - 列出 asp.net mvc 中文件夹中的文件

asp.net-mvc-3 - 如何在 Razor View 引擎中注册程序集