first edit
This commit is contained in:
115
XLIMS.SERVICES/GenericRepository.cs
Normal file
115
XLIMS.SERVICES/GenericRepository.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using XLIMS.CONTRACT;
|
||||
|
||||
namespace XLIMS.SERVICES
|
||||
{
|
||||
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
|
||||
{
|
||||
protected readonly DbContext _context;
|
||||
protected readonly DbSet<TEntity> _dbSet;
|
||||
public event Action SetChanged;
|
||||
|
||||
public GenericRepository(DbContext context)
|
||||
{
|
||||
_context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
_dbSet = context.Set<TEntity>();
|
||||
}
|
||||
|
||||
public async Task<TEntity> GetByIdAsync(int id)
|
||||
{
|
||||
return await _dbSet.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> GetAllAsync()
|
||||
{
|
||||
return await _dbSet.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> WhereAsync(Expression<Func<TEntity, bool>> predicate)
|
||||
{
|
||||
return await _dbSet.Where(predicate).ToListAsync();
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
|
||||
{
|
||||
return _dbSet.Where(predicate);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate)
|
||||
{
|
||||
return await _dbSet.Where(predicate).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<TEntity> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate)
|
||||
{
|
||||
return await _dbSet.SingleOrDefaultAsync(predicate);
|
||||
}
|
||||
|
||||
public async Task AddAsync(TEntity entity)
|
||||
{
|
||||
await _dbSet.AddAsync(entity);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task AddRangeAsync(IEnumerable<TEntity> entities)
|
||||
{
|
||||
await _dbSet.AddRangeAsync(entities);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(TEntity entity)
|
||||
{
|
||||
_dbSet.Update(entity);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task UpdateRangeAsync(IEnumerable<TEntity> entities)
|
||||
{
|
||||
_dbSet.UpdateRange(entities);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task RemoveAsync(TEntity entity)
|
||||
{
|
||||
_dbSet.Remove(entity);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task RemoveRangeAsync(IEnumerable<TEntity> entities)
|
||||
{
|
||||
_dbSet.RemoveRange(entities);
|
||||
await SaveChangesAsync();
|
||||
SetChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate = null)
|
||||
{
|
||||
if (predicate == null)
|
||||
return await _dbSet.CountAsync();
|
||||
|
||||
return await _dbSet.CountAsync(predicate);
|
||||
}
|
||||
|
||||
public async Task<int> SaveChangesAsync()
|
||||
{
|
||||
return await _context.SaveChangesAsync();
|
||||
|
||||
}
|
||||
|
||||
public int SaveChanges()
|
||||
{
|
||||
return _context.SaveChanges();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user