ini
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
|
||||
namespace AuthHex.Infra.UoW
|
||||
{
|
||||
public class EFUnitOfWork : IUnitOfWork
|
||||
{
|
||||
private readonly AppDBContext _dbContext;
|
||||
private IDbContextTransaction? _transaction;
|
||||
|
||||
public EFUnitOfWork(AppDBContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task BeginAsync(CancellationToken ct = default)
|
||||
{
|
||||
if (_transaction == null)
|
||||
_transaction = await _dbContext.Database.BeginTransactionAsync(ct);
|
||||
}
|
||||
|
||||
public async Task SaveChangesAsync(CancellationToken ct = default)
|
||||
{
|
||||
await _dbContext.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task CommitAsync(CancellationToken ct = default)
|
||||
{
|
||||
await _dbContext.SaveChangesAsync(ct);
|
||||
|
||||
if (_transaction != null)
|
||||
{
|
||||
await _transaction.CommitAsync(ct);
|
||||
await _transaction.DisposeAsync();
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RollbackAsync(CancellationToken ct = default)
|
||||
{
|
||||
if (_transaction != null)
|
||||
{
|
||||
await _transaction.RollbackAsync(ct);
|
||||
await _transaction.DisposeAsync();
|
||||
_transaction = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user