4f573da374
- 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>
75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
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();
|
|
}
|
|
}
|
|
}
|