Spaces:
Sleeping
Sleeping
File size: 1,346 Bytes
c2908d2 a19fd25 c2908d2 8ded86b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | using Microsoft.EntityFrameworkCore;
using OilShopManagement.Data;
using System.Linq.Expressions;
namespace OilShopManagement.Repositories;
public class Repository<T> : IRepository<T> where T : class
{
protected readonly ApplicationDbContext _context;
protected readonly DbSet<T> _dbSet;
public Repository(ApplicationDbContext context)
{
_context = context;
_dbSet = context.Set<T>();
}
public virtual async Task<IEnumerable<T>> GetAllAsync() =>
await _dbSet.ToListAsync();
public virtual async Task<T?> GetByIdAsync(int id) =>
await _dbSet.FindAsync(id);
public virtual async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate) =>
await _dbSet.Where(predicate).ToListAsync();
public virtual async Task AddAsync(T entity) =>
await _dbSet.AddAsync(entity);
public virtual void Update(T entity) =>
_dbSet.Update(entity);
public virtual void Remove(T entity) =>
_dbSet.Remove(entity);
public virtual async Task<bool> SaveChangesAsync() =>
await _context.SaveChangesAsync() > 0;
public virtual async Task<int> CountAsync() =>
await _dbSet.CountAsync();
public virtual async Task<int> CountAsync(Expression<Func<T, bool>> predicate) =>
await _dbSet.CountAsync(predicate);
}
|