1
\$\begingroup\$

I want to create some BaseRepository for EntityFrameworkCode and I've tried construct this Repository based on code from AspNetBoilerplate but with some modifications:

I need revision especially part with Insert, Update, Delete - is it correct implementation?

        public class BaseRepository<TDbContext, TEntity>
         where TEntity : class
         where TDbContext : DbContext
    {
        private readonly TDbContext _dbContext;

        public virtual DbSet<TEntity> Table => _dbContext.Set<TEntity>();

        public BaseRepository(TDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public virtual IQueryable<TEntity> GetAll()
        {
            return GetAllIncluding();
        }

        public virtual IQueryable<TEntity> GetAllNoTracking()
        {
            return GetAllIncluding().AsNoTracking();
        }

        public virtual IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors)
        {
            var query = Table.AsQueryable();

            if (!propertySelectors.IsNullOrEmpty())
            {
                foreach (var propertySelector in propertySelectors)
                {
                    query = query.Include(propertySelector);
                }
            }

            return query;
        }

        public virtual async Task<List<TEntity>> GetAllListAsync()
        {
            return await GetAll().ToListAsync();
        }

        public async Task<List<TEntity>> GetPagedListIncludingAsync(int page, int pageSize, params Expression<Func<TEntity, object>>[] propertySelectors)
        {
            var query = Table.AsQueryable();

            if (!propertySelectors.IsNullOrEmpty())
            {
                foreach (var propertySelector in propertySelectors)
                {
                    query = query.Include(propertySelector);
                }
            }

            return await query.Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        public virtual List<TEntity> GetPagedList(int page, int pageSize)
        {
            return GetAll().Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }

        public virtual async Task<List<TEntity>> GetPagedListAsync(int page, int pageSize)
        {
            return await GetAll().Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        public virtual async Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await GetAll().Where(predicate).ToListAsync();
        }

        public List<TEntity> GetPagedListIncluding(int page, int pageSize, params Expression<Func<TEntity, object>>[] propertySelectors)
        {
            var query = Table.AsQueryable();

            if (!propertySelectors.IsNullOrEmpty())
            {
                foreach (var propertySelector in propertySelectors)
                {
                    query = query.Include(propertySelector);
                }
            }

            return query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }

        public virtual TEntity Get(Expression<Func<TEntity, bool>> predicate)
        {
            return GetAll().Where(predicate).SingleOrDefault();
        }

        public virtual async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await GetAll().Where(predicate).SingleOrDefaultAsync();
        }

        public virtual TEntity Insert(TEntity entity)
        {
            Table.Add(entity);

            _dbContext.SaveChanges();

            return entity;
        }

        public virtual async Task<TEntity> InsertAsync(TEntity entity)
        {
            _dbContext.Add(entity);

            await _dbContext.SaveChangesAsync();

            return entity;
        }

        public virtual IList<TEntity> InsertRange(IList<TEntity> entities)
        {
            Table.AddRange(entities);

            _dbContext.SaveChanges();

            return entities;
        }

        public virtual async Task<IList<TEntity>> InsertRangeAsync(IList<TEntity> entities)
        {
            Table.AddRange(entities);

            await _dbContext.SaveChangesAsync();

            return entities;
        }

        public virtual TEntity Update(TEntity entity)
        {
            Table.Update(entity);
            _dbContext.SaveChanges();

            return entity;
        }

        public virtual async Task<TEntity> UpdateAsync(TEntity entity)
        {
            Table.Update(entity);
            await _dbContext.SaveChangesAsync();

            return entity;
        }

        public virtual IList<TEntity> UpdateRange(IList<TEntity> entities)
        {
            Table.UpdateRange(entities);
            _dbContext.SaveChanges();

            return entities;
        }

        public virtual async Task<IList<TEntity>> UpdateRangeAsync(IList<TEntity> entities)
        {
            Table.UpdateRange(entities);
            await _dbContext.SaveChangesAsync();

            return entities;
        }

        public virtual void Delete(TEntity entity)
        {
            Table.Remove(entity);

            _dbContext.SaveChanges();
        }

        public virtual async Task DeleteAsync(TEntity entity)
        {
            Table.Remove(entity);

            await _dbContext.SaveChangesAsync();
        }

        public virtual void DeleteRange(IList<TEntity> entities)
        {
            Table.RemoveRange(entities);

            _dbContext.SaveChanges();
        }

        public virtual async Task DeleteRangeAsync(IList<TEntity> entities)
        {
            Table.RemoveRange(entities);

            await _dbContext.SaveChangesAsync();
        }

        public virtual int Count()
        {
            return GetAll().Count();
        }

        public virtual async Task<int> CountAsync()
        {
            return await GetAll().CountAsync();
        }

        public virtual int Count(Expression<Func<TEntity, bool>> predicate)
        {
            return GetAll().Where(predicate).Count();
        }

        public virtual async Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await GetAll().Where(predicate).CountAsync();
        }

        public virtual long LongCount()
        {
            return GetAll().LongCount();
        }

        public virtual async Task<long> LongCountAsync()
        {
            return await GetAll().LongCountAsync();
        }

        public virtual long LongCount(Expression<Func<TEntity, bool>> predicate)
        {
            return GetAll().Where(predicate).LongCount();
        }

        public virtual async Task<long> LongCountAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await GetAll().Where(predicate).LongCountAsync();
        }

        public virtual DbContext GetDbContext()
        {
            return _dbContext;
        }
}
\$\endgroup\$
  • \$\begingroup\$ Modifications for what purpose? \$\endgroup\$ – 200_success Oct 19 '17 at 13:59
  • \$\begingroup\$ Good question :) In AspNetBoilerplate is necessary to implement the interface - IEntity for each entity and I use an external db model and can't implement IEntity for every entity. \$\endgroup\$ – Jenan Oct 19 '17 at 14:04
  • \$\begingroup\$ What did you win by implementing it this way? Isn't it easier to just have a class that just provides only a few queries that you actually need? \$\endgroup\$ – t3chb0t Oct 19 '17 at 17:21
  • \$\begingroup\$ Repositories shouldn't save changes. \$\endgroup\$ – Gert Arnold Oct 19 '17 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.