refactor: rename NotificationEfRepository to EfRepository
Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
||||
|
||||
internal abstract class EfRepository<TEntity>
|
||||
where TEntity : class
|
||||
{
|
||||
protected NotificationDbContext DbContext { get; }
|
||||
protected DbSet<TEntity> DbSet { get; }
|
||||
|
||||
protected EfRepository(NotificationDbContext dbContext)
|
||||
{
|
||||
DbContext = dbContext;
|
||||
DbSet = dbContext.Set<TEntity>();
|
||||
}
|
||||
|
||||
protected async Task AddAsync(TEntity entity, CancellationToken ct = default)
|
||||
{
|
||||
await DbSet.AddAsync(entity, ct);
|
||||
await DbContext.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
protected async Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken ct = default)
|
||||
{
|
||||
await DbSet.AddRangeAsync(entities, ct);
|
||||
await DbContext.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
protected async Task UpdateAsync(TEntity entity, CancellationToken ct = default)
|
||||
{
|
||||
DbSet.Update(entity);
|
||||
await DbContext.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
protected async Task DeleteAsync(TEntity entity, CancellationToken ct = default)
|
||||
{
|
||||
DbSet.Remove(entity);
|
||||
await DbContext.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
protected async Task DeleteRangeAsync(IEnumerable<TEntity> entities, CancellationToken ct = default)
|
||||
{
|
||||
DbSet.RemoveRange(entities);
|
||||
await DbContext.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
protected Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken ct = default)
|
||||
{
|
||||
return DbSet.AnyAsync(predicate, ct);
|
||||
}
|
||||
|
||||
protected Task SaveAsync(CancellationToken ct = default)
|
||||
{
|
||||
return DbContext.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user