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,23 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
||||
|
||||
internal sealed class EfTransactionAdapter : ITransaction
|
||||
{
|
||||
private readonly IDbContextTransaction _transaction;
|
||||
|
||||
internal EfTransactionAdapter(IDbContextTransaction transaction)
|
||||
{
|
||||
_transaction = transaction;
|
||||
}
|
||||
|
||||
public Task CommitAsync(CancellationToken cancellationToken = default) =>
|
||||
_transaction.CommitAsync(cancellationToken);
|
||||
|
||||
public Task RollbackAsync(CancellationToken cancellationToken = default) =>
|
||||
_transaction.RollbackAsync(cancellationToken);
|
||||
|
||||
public ValueTask DisposeAsync() =>
|
||||
_transaction.DisposeAsync();
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
||||
|
||||
internal abstract class EfUnitOfWork<TDbContext> : IUnitOfWork
|
||||
where TDbContext : DbContext
|
||||
{
|
||||
private readonly TDbContext _context;
|
||||
private EfTransactionAdapter? _currentTransaction;
|
||||
|
||||
protected EfUnitOfWork(TDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<ITransaction> BeginTransactionAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_currentTransaction != null)
|
||||
return _currentTransaction;
|
||||
|
||||
IDbContextTransaction tx = await _context.Database.BeginTransactionAsync(cancellationToken);
|
||||
_currentTransaction = new EfTransactionAdapter(tx);
|
||||
return _currentTransaction;
|
||||
}
|
||||
|
||||
public ITransaction? GetCurrentTransaction() => _currentTransaction;
|
||||
|
||||
public async Task ExecuteInTransactionAsync(Func<Task> action)
|
||||
{
|
||||
ITransaction? existing = GetCurrentTransaction();
|
||||
bool ownsTransaction = existing is null;
|
||||
ITransaction tx = existing ?? await BeginTransactionAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await action();
|
||||
if (ownsTransaction) await tx.CommitAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (ownsTransaction) await tx.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (ownsTransaction) await tx.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TResult> ExecuteInTransactionAsync<TResult>(Func<Task<TResult>> action)
|
||||
{
|
||||
ITransaction? existing = GetCurrentTransaction();
|
||||
bool ownsTransaction = existing is null;
|
||||
ITransaction tx = existing ?? await BeginTransactionAsync();
|
||||
|
||||
try
|
||||
{
|
||||
TResult result = await action();
|
||||
if (ownsTransaction) await tx.CommitAsync();
|
||||
return result;
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (ownsTransaction) await tx.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (ownsTransaction) await tx.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
||||
|
||||
internal sealed class NotificationUnitOfWork : EfUnitOfWork<NotificationDbContext>
|
||||
{
|
||||
public NotificationUnitOfWork(NotificationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user