.net core2.x

概要:在搭建框架,顺手说下写下,关于Repository,可能你理解了,可能你还不理解,可能与不可能不是重点,重点是感兴趣就看看吧。

  1.仓储(Repository)是什么?

  看下翻译:仓库; 贮藏室; 博物馆; 亲信;(百度翻译),说他是亲信?怎么理解?我们在初始化上下文之后,访问数据库的操作就是由他操作的,提供数据库的增删改查操作,它的存在 起到了 承上启下的作用 ,承上:隔离了业务直接访问上下文对象; 启下:

业务操作,用它提供CRUD操作。

  所以简要概括就是这几点:

  a) 隔离上下文,提供统一的操作方法(CRUD)

  b)为业务开发(程序开发)提供统一的规范

  2.相关示例

  见示例代码:

public class Repository<TEntity, TKey> : IRepository<TEntity, TKey>
        where TEntity : class, IEntity<TKey>
    {
        #region ctor
        public Repository(IUnitOfWork unitOfWork)
        {
            _dbContext = unitOfWork.GetDbContext as DbContext;
            _dbSet = _dbContext.Set<TEntity>();
        }
        #endregion

        #region fields
        private readonly DbSet<TEntity> _dbSet;
        private readonly DbContext _dbContext;
        #endregion

        #region query
        public TEntity GetByKey(TKey key)
        {
            return _dbSet.Find(key);
        }

        public async Task<TEntity> GetByKeyAsync(TKey key)
        {
            return await _dbSet.FindAsync(key);
        }

        public IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> expression)
        {
            return _dbSet.Where(expression);
        }

        #endregion

        #region insert
        public void Insert(TEntity entity)
        {
            _dbSet.Add(entity);
        }

        public void Insert(IEnumerable<TEntity> entities)
        {
            _dbSet.AddRange(entities);
        }

        public async Task InsertAsync(TEntity entity)
        {
            await _dbSet.AddAsync(entity);
        }

        public async Task InsertAsync(IEnumerable<TEntity> entities)
        {
            await _dbSet.AddRangeAsync(entities);
        }

        #endregion

        #region update
        public void Remove(TEntity entity)
        {
            _dbSet.Remove(entity);
        }

        public void Remove(Expression<Func<TEntity, bool>> expression)
        {
            var entities = _dbSet.AsNoTracking().Where(expression).ToList();
            _dbSet.RemoveRange(entities);
        }

        #endregion

        #region remove
        public void Update(TEntity entity)
        {
            _dbSet.Update(entity);
        }

        public void Update(IEnumerable<TEntity> entities)
        {
            _dbSet.UpdateRange(entities);
        }

        #endregion
    }
View Code

  以上为一个示例代码,如果有其他需求自行修改,比如:根据某个属性进行编辑操作等。这里涉及到一个 uow对象的注入,它提供了上下文对象(见上一篇的 uow模式)

  这样以来,在service层如果我们想要操作数据库的 增删改查 操作,只需要将 repository注入即可,上面的代码对应的接口实现如下:

public interface IRepository<TEntity, TKey>
        where TEntity : class, IEntity<TKey>
    {
        #region Query
        TEntity GetByKey(TKey key);
        Task<TEntity> GetByKeyAsync(TKey key);
        IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> expression);

        #endregion

        #region Insert
        void Insert(TEntity entity);
        void Insert(IEnumerable<TEntity> entities);
        Task InsertAsync(TEntity entity);
        Task InsertAsync(IEnumerable<TEntity> entities);
        #endregion

        #region Update
        void Update(TEntity entity);
        void Update(IEnumerable<TEntity> entities);

        #endregion

        #region Remove
        void Remove(TEntity entity);
        void Remove(Expression<Func<TEntity, bool>> expression);
        
        #endregion

    }
View Code

  其中你可能会有一个疑问 uow和repository有啥关系? 可以见我之前有写的 一个说明 二者关系

  

完!

  

   

  

原文地址:https://www.cnblogs.com/Tmc-Blog/p/9919078.html