feat: add repository layer with IUnitOfWork and fixed EF base
- ITransaction, IUnitOfWork in DAL.Abstract - EfTransactionAdapter, EfUnitOfWork<TDbContext>, NotificationUnitOfWork in DAL.EF - NotificationEfRepository<TEntity>: async-only base, fixed Exists (AnyAsync), fixed batch Add (AddRangeAsync), single SaveChangesAsync per operation - TemplateRepository, ProviderRepository, ProviderUsageRepository - ProviderUsageRepository.IncrementAsync uses atomic PostgreSQL upsert - ProviderRepository deserializes settings polymorphically via ProviderType discriminator Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
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) =>
|
||||
DbSet.AnyAsync(predicate, ct);
|
||||
|
||||
protected Task SaveAsync(CancellationToken ct = default) =>
|
||||
DbContext.SaveChangesAsync(ct);
|
||||
}
|
||||
Reference in New Issue
Block a user