【问题标题】:how can I write on generic class for all Repository我如何在所有存储库的通用类上编写
【发布时间】:2016-08-22 15:45:42
【问题描述】:

我想为所有存储库编写一个类泛型

我有一个类实体(产品、价格、....)

和接口 IRepository

 public  interface IRepository<T> 
    {
        Dictionary<int, T> myProduct { get; set; }

        IEnumerable<T> List { get; }
        void Add(T entity);
        void Delete(T entity,int key);
        void Update(T entity,int key);
        T FindById(int Id);

    }

我可以为每个业务对象编写我的存储库,例如 ProudductRepository、priceRepository、.....

但我需要像这样实现一个通用的东西

 public class Repository<E, C> : IRepository<E>

我将它用于我的所有业务对象,而不是一个 foreach 实体

【问题讨论】:

  • 好的,你可以这样做。现在你的问题是什么?
  • '但我需要实现为泛型' -> 为什么?
  • 我知道可以这样做,但是怎么做
  • 顺便说一句,如果您在实体框架之上使用此抽象,我个人建议不要这样做。通用存储库和 EF 的工作单元往往会在功能方面给您留下巨大的漏洞。
  • 是的,我已经使用了基于存储库的实体框架。但我需要它的另一部分

标签: c# entity-framework repository repository-pattern


【解决方案1】:

您可以如下所示进行操作。这只是一个示例。

创建通用存储库

namespace ContosoUniversity.DAL
{
    public class GenericRepository<TEntity> where TEntity : class
    {
        internal SchoolContext context;
        internal DbSet<TEntity> dbSet;

        public GenericRepository(SchoolContext context)
        {
            this.context = context;
            this.dbSet = context.Set<TEntity>();
        }

        public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "")
        {
            IQueryable<TEntity> query = dbSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }

        public virtual TEntity GetByID(object id)
        {
            return dbSet.Find(id);
        }

        public virtual void Insert(TEntity entity)
        {
            dbSet.Add(entity);
        }

        public virtual void Delete(object id)
        {
            TEntity entityToDelete = dbSet.Find(id);
            Delete(entityToDelete);
        }

        public virtual void Delete(TEntity entityToDelete)
        {
            if (context.Entry(entityToDelete).State == EntityState.Detached)
            {
                dbSet.Attach(entityToDelete);
            }
            dbSet.Remove(entityToDelete);
        }

        public virtual void Update(TEntity entityToUpdate)
        {
            dbSet.Attach(entityToUpdate);
            context.Entry(entityToUpdate).State = EntityState.Modified;
        }
    }
}

请在Implement a Generic Repository and a Unit of Work Class 文章中阅读更多相关信息。

更多文章给你:

Generic Repository and UnitofWork patterns in MVC

Generic Repository and Unit of Work Pattern

【讨论】:

    猜你喜欢
    • 2019-05-10
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多