Cool UnitOfWork convenience method (:

This commit is contained in:
lumijiez
2025-06-21 21:15:10 +03:00
parent ec78744d19
commit d9dfafe07a
4 changed files with 52 additions and 35 deletions

View File

@@ -43,4 +43,37 @@ public class UnitOfWork(
{
await context.Database.RollbackTransactionAsync(cancellationToken);
}
public async Task<T> TransactAsync<T>(Func<Task<T>> operation, CancellationToken cancellationToken = default)
{
await BeginTransactionAsync(cancellationToken);
try
{
var result = await operation();
await SaveAsync(cancellationToken);
await CommitTransactionAsync(cancellationToken);
return result;
}
catch
{
await RollbackTransactionAsync(cancellationToken);
throw;
}
}
public async Task TransactAsync(Func<Task> operation, CancellationToken cancellationToken = default)
{
await BeginTransactionAsync(cancellationToken);
try
{
await operation();
await SaveAsync(cancellationToken);
await CommitTransactionAsync(cancellationToken);
}
catch
{
await RollbackTransactionAsync(cancellationToken);
throw;
}
}
}