Commit 0b7ddf50 authored by hasan khaddour's avatar hasan khaddour

fix unit of work

parent 8f8ad4cc
using MediatR;
using Microsoft.EntityFrameworkCore.Storage;
using PSManagement.SharedKernel.Entities;
using PSManagement.SharedKernel.Events;
using PSManagement.SharedKernel.Interfaces;
......@@ -17,6 +18,7 @@ namespace PSManagement.Infrastructure.Persistence.UoW
private readonly AppDbContext _dbContext;
private readonly IMediator _mediator;
private IDbContextTransaction _transaction;
#endregion Dependencies
......@@ -29,13 +31,45 @@ namespace PSManagement.Infrastructure.Persistence.UoW
#endregion Constructor
#region UOW Operations
public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
await DispatchEventsAsync();
await _dbContext.SaveChangesAsync(cancellationToken);
try
{
_transaction?.Commit();
}
catch
{
await Rollback();
// throw;
}
finally
{
DisposeTransaction();
}
}
public void BeginTransaction()
{
_transaction =_dbContext.Database.BeginTransaction();
}
public async Task Rollback()
{
await _transaction.RollbackAsync();
}
private void DisposeTransaction()
{
if (_transaction != null)
{
_transaction.Dispose();
_transaction = null;
}
}
#endregion UOW Operations
#region Process Events
private async Task DispatchEventsAsync()
{
......@@ -112,6 +146,7 @@ namespace PSManagement.Infrastructure.Persistence.UoW
{
_dbContext.Dispose();
}
#endregion Clear And Dispose
}
}
......@@ -10,5 +10,8 @@ namespace PSManagement.SharedKernel.Interfaces
public interface IUnitOfWork
{
Task SaveChangesAsync(CancellationToken cancellationToken = default);
void BeginTransaction();
Task Rollback();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment