扩展Entity Framework的工作单元模式

3
我正在尝试将“工作单元(Unit of Work)”应用到我的项目中,并有一些快速问题。 我使用了这个教程中所示的类:链接
因此当我实现它时,假设我将类“Users”作为“GenericRepository”,那么如果有一些我想要添加到“Users”类但不属于“GenericRepository”的项目怎么办?
如何创建另一个接口并使用某种继承类型,以便我仍然可以从“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;
        }
    }
}

To which I recieve this error:

'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>'.
1个回答

3
假设您有一个基础/通用存储库接口,其外观如下:
 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

如果您没有基础存储库类,那么您必须在两个存储库中实现所有方法。也许这就是为什么您会收到异常的原因? - Imran Rashid
@ImranRashid 这是我得到的最终错误:'Comics.Models.Category' 不能用作泛型类型或方法 'Comics.DAL.Interfaces.GenericRepository<TEntity>' 中的类型参数 'TEntity'。从 'Comics.Models.Category' 到 'Comics.DAL.Interfaces.IGenericRepository<Comics.Models.Category>' 没有隐式引用转换。我已经发布了我的代码。 - Moe Bataineh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接