Files
hrynco-notification-service/HrynCo.NotificationService.DAL.EF/Core/NotificationEfRepository.cs
T
Anatolii Grynchuk c2a4f3b9d7 fix: inherit EF entities from Entity base class
- TemplateEntity, ProviderEntity, ProviderUsageEntity now inherit Entity (from DAL.Abstract)
- Removes duplicated Id, Created, Updated properties from each entity

Ref: IT-628

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-01 23:27:38 +03:00

57 lines
1.7 KiB
C#

using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
namespace HrynCo.NotificationService.DAL.EF.Core;
internal abstract class NotificationEfRepository<TEntity>
where TEntity : class
{
protected NotificationDbContext DbContext { get; }
protected DbSet<TEntity> DbSet { get; }
protected NotificationEfRepository(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);
}
}