2017-03-14 18:02:28 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-09-14 15:33:39 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Design;
|
2018-10-03 17:54:15 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
2017-01-25 17:10:08 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
2017-01-30 15:46:43 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork;
|
2017-03-14 18:02:28 +01:00
|
|
|
|
using Ordering.Infrastructure;
|
2017-08-31 17:16:28 +02:00
|
|
|
|
using Ordering.Infrastructure.EntityConfigurations;
|
2017-01-25 17:10:08 +01:00
|
|
|
|
using System;
|
2018-10-03 17:54:15 +02:00
|
|
|
|
using System.Data;
|
2017-03-14 18:02:28 +01:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-01-25 17:10:08 +01:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
|
2016-11-22 18:40:47 +01:00
|
|
|
|
{
|
2017-11-07 16:53:41 -08:00
|
|
|
|
public class OrderingContext : DbContext, IUnitOfWork
|
2016-11-22 18:40:47 +01:00
|
|
|
|
{
|
2017-08-31 17:16:28 +02:00
|
|
|
|
public const string DEFAULT_SCHEMA = "ordering";
|
2016-11-22 18:40:47 +01:00
|
|
|
|
public DbSet<Order> Orders { get; set; }
|
|
|
|
|
public DbSet<OrderItem> OrderItems { get; set; }
|
2017-01-27 11:38:23 +01:00
|
|
|
|
public DbSet<PaymentMethod> Payments { get; set; }
|
2016-11-22 18:40:47 +01:00
|
|
|
|
public DbSet<Buyer> Buyers { get; set; }
|
2016-11-24 14:59:25 +01:00
|
|
|
|
public DbSet<CardType> CardTypes { get; set; }
|
2016-11-22 18:40:47 +01:00
|
|
|
|
public DbSet<OrderStatus> OrderStatus { get; set; }
|
|
|
|
|
|
2017-03-14 18:02:28 +01:00
|
|
|
|
private readonly IMediator _mediator;
|
2018-10-03 17:54:15 +02:00
|
|
|
|
private IDbContextTransaction _currentTransaction;
|
2017-03-14 18:02:28 +01:00
|
|
|
|
|
2019-08-12 08:27:19 +02:00
|
|
|
|
public OrderingContext(DbContextOptions<OrderingContext> options) : base(options) { }
|
2017-08-31 17:16:28 +02:00
|
|
|
|
|
2019-05-07 21:29:37 +01:00
|
|
|
|
public IDbContextTransaction GetCurrentTransaction() => _currentTransaction;
|
2018-10-03 23:45:41 +02:00
|
|
|
|
|
2019-02-21 17:19:31 +00:00
|
|
|
|
public bool HasActiveTransaction => _currentTransaction != null;
|
|
|
|
|
|
2017-09-14 13:43:41 +02:00
|
|
|
|
public OrderingContext(DbContextOptions<OrderingContext> options, IMediator mediator) : base(options)
|
2017-03-14 18:02:28 +01:00
|
|
|
|
{
|
2017-04-17 12:28:12 +02:00
|
|
|
|
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
2017-05-11 13:44:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.Diagnostics.Debug.WriteLine("OrderingContext::ctor ->" + this.GetHashCode());
|
2017-03-14 18:02:28 +01:00
|
|
|
|
}
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
2017-08-31 17:16:28 +02:00
|
|
|
|
modelBuilder.ApplyConfiguration(new ClientRequestEntityTypeConfiguration());
|
|
|
|
|
modelBuilder.ApplyConfiguration(new PaymentMethodEntityTypeConfiguration());
|
|
|
|
|
modelBuilder.ApplyConfiguration(new OrderEntityTypeConfiguration());
|
|
|
|
|
modelBuilder.ApplyConfiguration(new OrderItemEntityTypeConfiguration());
|
|
|
|
|
modelBuilder.ApplyConfiguration(new CardTypeEntityTypeConfiguration());
|
|
|
|
|
modelBuilder.ApplyConfiguration(new OrderStatusEntityTypeConfiguration());
|
2019-02-21 17:19:31 +00:00
|
|
|
|
modelBuilder.ApplyConfiguration(new BuyerEntityTypeConfiguration());
|
2016-11-24 14:59:25 +01:00
|
|
|
|
}
|
2017-03-14 18:02:28 +01:00
|
|
|
|
|
2017-03-27 14:05:28 +02:00
|
|
|
|
public async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken))
|
2017-03-14 18:02:28 +01:00
|
|
|
|
{
|
2017-03-21 12:55:33 -07:00
|
|
|
|
// 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
|
2017-03-21 12:58:07 -07:00
|
|
|
|
// 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.
|
2017-03-21 12:55:33 -07:00
|
|
|
|
await _mediator.DispatchDomainEventsAsync(this);
|
|
|
|
|
|
2017-03-23 14:45:57 -07:00
|
|
|
|
// After executing this line all the changes (from the Command Handler and Domain Event Handlers)
|
2018-07-05 15:07:38 +09:00
|
|
|
|
// performed through the DbContext will be committed
|
2019-05-06 14:57:44 -05:00
|
|
|
|
var result = await base.SaveChangesAsync(cancellationToken);
|
2017-03-16 18:52:02 -07:00
|
|
|
|
|
2017-03-27 14:05:28 +02:00
|
|
|
|
return true;
|
2018-10-03 17:54:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 15:56:15 +00:00
|
|
|
|
public async Task<IDbContextTransaction> BeginTransactionAsync()
|
2018-10-03 17:54:15 +02:00
|
|
|
|
{
|
2019-02-21 17:19:31 +00:00
|
|
|
|
if (_currentTransaction != null) return null;
|
|
|
|
|
|
|
|
|
|
_currentTransaction = await Database.BeginTransactionAsync(IsolationLevel.ReadCommitted);
|
2019-02-21 15:56:15 +00:00
|
|
|
|
|
|
|
|
|
return _currentTransaction;
|
2018-10-03 17:54:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 17:19:31 +00:00
|
|
|
|
public async Task CommitTransactionAsync(IDbContextTransaction transaction)
|
2018-10-03 17:54:15 +02:00
|
|
|
|
{
|
2019-02-21 17:19:31 +00:00
|
|
|
|
if (transaction == null) throw new ArgumentNullException(nameof(transaction));
|
|
|
|
|
if (transaction != _currentTransaction) throw new InvalidOperationException($"Transaction {transaction.TransactionId} is not current");
|
|
|
|
|
|
2018-10-03 17:54:15 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await SaveChangesAsync();
|
2019-02-21 17:19:31 +00:00
|
|
|
|
transaction.Commit();
|
2018-10-03 17:54:15 +02:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
2017-09-14 15:33:39 +02:00
|
|
|
|
|
|
|
|
|
public class OrderingContextDesignFactory : IDesignTimeDbContextFactory<OrderingContext>
|
|
|
|
|
{
|
|
|
|
|
public OrderingContext CreateDbContext(string[] args)
|
|
|
|
|
{
|
|
|
|
|
var optionsBuilder = new DbContextOptionsBuilder<OrderingContext>()
|
|
|
|
|
.UseSqlServer("Server=.;Initial Catalog=Microsoft.eShopOnContainers.Services.OrderingDb;Integrated Security=true");
|
|
|
|
|
|
2019-02-21 17:19:31 +00:00
|
|
|
|
return new OrderingContext(optionsBuilder.Options, new NoMediator());
|
2017-09-14 15:33:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class NoMediator : IMediator
|
|
|
|
|
{
|
|
|
|
|
public Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default(CancellationToken)) where TNotification : INotification
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default(CancellationToken))
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult<TResponse>(default(TResponse));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task Send(IRequest request, CancellationToken cancellationToken = default(CancellationToken))
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|