c# - 扩展 Entity Framework 的工作单元

标签 c# asp.net-mvc entity-framework

我正在尝试将 Unit of Work 实现到我的项目中,并有一些快速的问题。我正在使用显示的类 by this tutorial :

因此,当我实现它时,假设我将类 Users 设为 GenericRepository。如果有一些项目我想添加到 Users 类但不是 GenericRepository 的一部分怎么办。

如何创建另一个接口(interface)并使用某种类型的继承,这样我仍然可以从 GenericRepository 中获取内容以及我想要的新功能。

我基本上想扩展它。

public interface ICategoryRepository : IGenericRepository<Category>
{
    IEnumerable<LocalComicCategoriesModel> GetCategories();
    IEnumerable<LocalComicCategoriesModel> GetCategoriesByComicID(int comicid, bool appendCategories);
}

public class CategoryRepository : GenericRepository<Category>, ICategoryRepository
{
    new ComicEntities context;

    public CategoryRepository(ComicEntities context) : base(context)
    {
        this.context = context;

    }

    public IEnumerable<LocalComicCategoriesModel> GetCategories()
    {
        return context.Categorys.Select(i => new LocalComicCategoriesModel { Id = i.Category_Id, Title = i.Name });
    }

    public IEnumerable<LocalComicCategoriesModel> GetCategoriesByComicID(int comicid, bool appendCategories)
    {
        if (appendCategories == true)
        {
            IEnumerable<LocalComicCategoriesModel> query = from tbcat in context.Categorys
                                                           join tbitem_cat in context.ComicCategorys.Where(i => i.Comic_Id == comicid)
                                                           on tbcat.Category_Id equals tbitem_cat.Category_Id into ct
                                                           from tbitem_cat in ct.DefaultIfEmpty()
                                                           select new LocalComicCategoriesModel
                                                           {
                                                               Id = tbcat.Category_Id,
                                                               Title = tbcat.Name,
                                                               isChecked = tbitem_cat == null ? false : true
                                                           };

            return query;
        }
        else
        {
            IEnumerable<LocalComicCategoriesModel> returnedCategories = from t in context.Categorys
                                                                        join v in context.ComicCategorys on t.Category_Id equals v.Category_Id
                                                                        where v.Comic_Id == comicid
                                                                        select new LocalComicCategoriesModel
                                                                        {
                                                                            Id = v.Category_Id,
                                                                            isChecked = true,
                                                                            Title = t.Name
                                                                        };

            return returnedCategories;
        }
    }
}

我收到这个错误:

'Comics.Models.Category' cannot be used as type parameter 'TEntity' in the generic type or method 'Comics.DAL.Interfaces.GenericRepository<TEntity>'. There is no implicit reference conversion from 'Comics.Models.Category' to 'Comics.DAL.Interfaces.IGenericRepository<Comics.Models.Category>'.

最佳答案

假设您有一个如下所示的基本/通用存储库界面

 public interface IRepository<T> where T : class
 {
     IQueryable<T> All();
     IQueryable<T> Find(Expression<Func<T, bool>> predicate);        
     T GetById(int id);

     // and the rest of your methods
 }

如果你有一个“普通”类,你想为其实现这个通用存储库,你可以这样做

IRepository<MyClass> MyClassRepository

现在,对于需要额外功能的类(比如你的 User 类),你可以像这样创建一个额外的存储库:

public interface IUserRepository : IRepository<User>
    {
        IQueryable<User> AllAuthorized();
        IQueryable<User> AllConfirmed();
    }

然后要使用它,从您的通用存储库继承并让它实现您的 IUserRepository界面。

    public class UserRepository : Repository<User>, IUserRepository
    {       
        public IQueryable<User> AllAuthorized()
        {
            // implement here
        }

        public IQueryable<User> AllConfirmed()
        {
           // implement here
        }
     }

现在不是实例化 IRepository<User> ,您只需使用 IUserRepository

关于c# - 扩展 Entity Framework 的工作单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21652537/

相关文章:

c# - 尝试将 StreamWriter 打开到命名管道时 Mono 挂起

c# - 使用 TCPDF 在 ASP.NET MVC 中生成 PDF

c# - 无法加载文件或程序集 System.Web.Mvc 或其依赖项之一

c# - EntityFramework LINQ 字符串顺序比较

c# - 如何使用 Entity Framework 和存储过程来避免 TimeOut 异常?

C# udp 套接字没有收到整个消息

c# - C# 中的 SET IDENTITY_INSERT 用法

javascript - 如何在不离开 ASP.NET C# 焦点的情况下触发 TextChange 事件?

javascript - 网格中的 TreeView 给出 "Unexpected token :"

c# - 一对零或一关系,其中 FK 是从属表上的复合 PK 的一部分