80 lines
3.3 KiB
C#
Raw Normal View History

2017-03-14 18:02:28 +01:00
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork;
2017-03-14 18:02:28 +01:00
using Ordering.Infrastructure;
using Ordering.Infrastructure.EntityConfigurations;
using System;
2017-03-14 18:02:28 +01:00
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
2016-11-22 18:40:47 +01:00
{
public class OrderingContext
: DbContext,IUnitOfWork
2016-11-22 18:40:47 +01: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; }
public DbSet<PaymentMethod> Payments { get; set; }
2016-11-22 18:40:47 +01:00
public DbSet<Buyer> Buyers { get; set; }
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;
public static OrderingContext CreateForEFDesignTools(DbContextOptions options)
{
return new OrderingContext(options);
}
private OrderingContext(DbContextOptions options) : base (options) { }
2017-03-14 18:02:28 +01:00
public OrderingContext(DbContextOptions options, IMediator mediator) : base(options)
{
2017-04-17 12:28:12 +02:00
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
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)
{
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());
}
2017-03-14 18:02:28 +01:00
public async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken))
2017-03-14 18:02:28 +01: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.
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)
// performed throught the DbContext will be commited
2017-03-14 18:02:28 +01:00
var result = await base.SaveChangesAsync();
return true;
2017-03-14 18:02:28 +01:00
}
2016-11-22 18:40:47 +01:00
}
}