namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; public class OrderingContext : DbContext, IUnitOfWork { public const string DEFAULT_SCHEMA = "ordering"; public DbSet Orders { get; set; } public DbSet OrderItems { get; set; } public DbSet Payments { get; set; } public DbSet Buyers { get; set; } public DbSet CardTypes { get; set; } public DbSet OrderStatus { get; set; } private readonly IMediator _mediator; private IDbContextTransaction _currentTransaction; public OrderingContext(DbContextOptions options) : base(options) { } public IDbContextTransaction GetCurrentTransaction() => _currentTransaction; public bool HasActiveTransaction => _currentTransaction != null; public OrderingContext(DbContextOptions options, IMediator mediator) : base(options) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); System.Diagnostics.Debug.WriteLine("OrderingContext::ctor ->" + this.GetHashCode()); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ApplyConfiguration(new ClientRequestEntityTypeConfiguration()); modelBuilder.ApplyConfiguration(new PaymentMethodEntityTypeConfiguration()); modelBuilder.ApplyConfiguration(new OrderEntityTypeConfiguration()); modelBuilder.ApplyConfiguration(new OrderItemEntityTypeConfiguration()); modelBuilder.ApplyConfiguration(new CardTypeEntityTypeConfiguration()); modelBuilder.ApplyConfiguration(new OrderStatusEntityTypeConfiguration()); modelBuilder.ApplyConfiguration(new BuyerEntityTypeConfiguration()); } public async Task SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken)) { // Dispatch Domain Events collection. // Choices: // A) Right BEFORE committing data (EF SaveChanges) into the DB will make a single transaction including // side effects from the domain event handlers which are using the same DbContext with "InstancePerLifetimeScope" or "scoped" lifetime // B) Right AFTER committing data (EF SaveChanges) into the DB will make multiple transactions. // You will need to handle eventual consistency and compensatory actions in case of failures in any of the Handlers. await _mediator.DispatchDomainEventsAsync(this); // After executing this line all the changes (from the Command Handler and Domain Event Handlers) // performed through the DbContext will be committed var result = await base.SaveChangesAsync(cancellationToken); return true; } public async Task BeginTransactionAsync() { if (_currentTransaction != null) return null; _currentTransaction = await Database.BeginTransactionAsync(IsolationLevel.ReadCommitted); return _currentTransaction; } public async Task CommitTransactionAsync(IDbContextTransaction transaction) { if (transaction == null) throw new ArgumentNullException(nameof(transaction)); if (transaction != _currentTransaction) throw new InvalidOperationException($"Transaction {transaction.TransactionId} is not current"); try { await SaveChangesAsync(); await transaction.CommitAsync(); } catch { RollbackTransaction(); throw; } finally { if (_currentTransaction != null) { _currentTransaction.Dispose(); _currentTransaction = null; } } } public void RollbackTransaction() { try { _currentTransaction?.Rollback(); } finally { if (_currentTransaction != null) { _currentTransaction.Dispose(); _currentTransaction = null; } } } } public class OrderingContextDesignFactory : IDesignTimeDbContextFactory { public OrderingContext CreateDbContext(string[] args) { var optionsBuilder = new DbContextOptionsBuilder() .UseSqlServer("Server=.;Initial Catalog=Microsoft.eShopOnContainers.Services.OrderingDb;Integrated Security=true"); return new OrderingContext(optionsBuilder.Options, new NoMediator()); } class NoMediator : IMediator { public IAsyncEnumerable CreateStream(IStreamRequest request, CancellationToken cancellationToken = default) { return default(IAsyncEnumerable); } public IAsyncEnumerable CreateStream(object request, CancellationToken cancellationToken = default) { return default(IAsyncEnumerable); } public Task Publish(TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification { return Task.CompletedTask; } public Task Publish(object notification, CancellationToken cancellationToken = default) { return Task.CompletedTask; } public Task Send(IRequest request, CancellationToken cancellationToken = default) { return Task.FromResult(default(TResponse)); } public Task Send(object request, CancellationToken cancellationToken = default) { return Task.FromResult(default(object)); } } }