mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-18 00:45:52 +08:00
Merge pull request #257 from meysamhadeli/refactor/refactor-db-context-base
refactor: Refactor db-context base
This commit is contained in:
commit
ad174d1be8
@ -24,45 +24,6 @@ public abstract class AppDbContextBase : DbContext, IDbContext
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task BeginTransactionalAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
_currentTransaction ??= await Database.BeginTransactionAsync(IsolationLevel.ReadCommitted, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
//ref: https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency#execution-strategies-and-transactions
|
|
||||||
public async Task CommitTransactionalAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await SaveChangesAsync(cancellationToken);
|
|
||||||
await _currentTransaction?.CommitAsync(cancellationToken)!;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
await _currentTransaction?.RollbackAsync(cancellationToken)!;
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_currentTransaction?.Dispose();
|
|
||||||
_currentTransaction = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public async Task RollbackTransactionAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await _currentTransaction?.RollbackAsync(cancellationToken)!;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_currentTransaction?.Dispose();
|
|
||||||
_currentTransaction = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//ref: https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency#execution-strategies-and-transactions
|
//ref: https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency#execution-strategies-and-transactions
|
||||||
public Task ExecuteTransactionalAsync(CancellationToken cancellationToken = default)
|
public Task ExecuteTransactionalAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -7,9 +7,6 @@ public interface IDbContext
|
|||||||
{
|
{
|
||||||
DbSet<TEntity> Set<TEntity>() where TEntity : class;
|
DbSet<TEntity> Set<TEntity>() where TEntity : class;
|
||||||
IReadOnlyList<IDomainEvent> GetDomainEvents();
|
IReadOnlyList<IDomainEvent> GetDomainEvents();
|
||||||
public Task BeginTransactionalAsync(CancellationToken cancellationToken = default);
|
|
||||||
public Task CommitTransactionalAsync(CancellationToken cancellationToken = default);
|
|
||||||
public Task RollbackTransactionAsync(CancellationToken cancellationToken = default);
|
|
||||||
Task ExecuteTransactionalAsync(CancellationToken cancellationToken = default);
|
Task ExecuteTransactionalAsync(CancellationToken cancellationToken = default);
|
||||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
|
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,8 +9,6 @@ using Xunit;
|
|||||||
|
|
||||||
namespace Integration.Test.Flight.Features;
|
namespace Integration.Test.Flight.Features;
|
||||||
|
|
||||||
using global::Flight.Flights.Features.CreatingFlight.V1;
|
|
||||||
|
|
||||||
public class CreateFlightTests : FlightIntegrationTestBase
|
public class CreateFlightTests : FlightIntegrationTestBase
|
||||||
{
|
{
|
||||||
public CreateFlightTests(
|
public CreateFlightTests(
|
||||||
|
|||||||
@ -34,45 +34,6 @@ public sealed class IdentityContext : IdentityDbContext<User, Role, Guid,
|
|||||||
builder.ToSnakeCaseTables();
|
builder.ToSnakeCaseTables();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task BeginTransactionalAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
_currentTransaction ??= await Database.BeginTransactionAsync(IsolationLevel.ReadCommitted, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
//ref: https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency#execution-strategies-and-transactions
|
|
||||||
public async Task CommitTransactionalAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await SaveChangesAsync(cancellationToken);
|
|
||||||
await _currentTransaction?.CommitAsync(cancellationToken)!;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
await _currentTransaction?.RollbackAsync(cancellationToken)!;
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_currentTransaction?.Dispose();
|
|
||||||
_currentTransaction = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public async Task RollbackTransactionAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await _currentTransaction?.RollbackAsync(cancellationToken)!;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_currentTransaction?.Dispose();
|
|
||||||
_currentTransaction = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//ref: https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency#execution-strategies-and-transactions
|
//ref: https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency#execution-strategies-and-transactions
|
||||||
public Task ExecuteTransactionalAsync(CancellationToken cancellationToken = default)
|
public Task ExecuteTransactionalAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user