| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- using Microsoft.EntityFrameworkCore;
- using Warhammer.DataLayer.Interfaces;
- using Warhammer.Models;
- namespace Warhammer.DataLayer
- {
- public class AbstractRepository<T> : IAbstractRepository<T> where T : BaseEntity
- {
- #region Fields
- protected DeathwatchDbContext DeathwatchDbContext;
- #endregion
- public AbstractRepository(DeathwatchDbContext deathwatchDbContext)
- {
- DeathwatchDbContext = deathwatchDbContext;
- }
- #region Public Methods
- public async Task<T> GetById(int id)
- {
- return await DeathwatchDbContext.Set<T>().FindAsync(id);
- }
- public async Task<T> FirstOrDefault(Expression<Func<T, bool>> predicate)
- {
- return await DeathwatchDbContext.Set<T>().FirstOrDefaultAsync(predicate);
- }
- public async Task Add(T entity)
- {
- // await DeathwatchDbContext.AddAsync(entity);
- await DeathwatchDbContext.Set<T>().AddAsync(entity);
- await DeathwatchDbContext.SaveChangesAsync();
- }
- public Task Update(T entity)
- {
- // In case AsNoTracking is used
- DeathwatchDbContext.Entry(entity).State = EntityState.Modified;
- return DeathwatchDbContext.SaveChangesAsync();
- }
- public Task Remove(T entity)
- {
- DeathwatchDbContext.Set<T>().Remove(entity);
- return DeathwatchDbContext.SaveChangesAsync();
- }
- public async Task<IEnumerable<T>> GetAll()
- {
- return await DeathwatchDbContext.Set<T>().ToListAsync();
- }
- public async Task<IEnumerable<T>> GetWhere(Expression<Func<T, bool>> predicate)
- {
- return await DeathwatchDbContext.Set<T>().Where(predicate).ToListAsync();
- }
- public Task<int> CountAll()
- {
- return DeathwatchDbContext.Set<T>().CountAsync();
- }
- public Task<int> CountWhere(Expression<Func<T, bool>> predicate)
- {
- return DeathwatchDbContext.Set<T>().CountAsync(predicate);
- }
- #endregion
- }
- }
|