2017-03-14 18:02:28 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-01-25 17:10:08 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
|
|
|
|
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-04-17 12:28:12 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
2017-03-14 18:02:28 +01:00
|
|
|
|
using Ordering.Infrastructure;
|
2017-01-25 17:10:08 +01:00
|
|
|
|
using System;
|
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
|
|
|
|
{
|
|
|
|
|
public class OrderingContext
|
2017-01-25 17:10:08 +01:00
|
|
|
|
: DbContext,IUnitOfWork
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
const string DEFAULT_SCHEMA = "ordering";
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2017-07-13 13:42:29 +02:00
|
|
|
|
|
|
|
|
|
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));
|
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-03-03 12:03:31 +01:00
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<ClientRequest>(ConfigureRequests);
|
2017-01-27 11:38:23 +01:00
|
|
|
|
modelBuilder.Entity<PaymentMethod>(ConfigurePayment);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
modelBuilder.Entity<Order>(ConfigureOrder);
|
|
|
|
|
modelBuilder.Entity<OrderItem>(ConfigureOrderItems);
|
2016-11-24 14:59:25 +01:00
|
|
|
|
modelBuilder.Entity<CardType>(ConfigureCardTypes);
|
|
|
|
|
modelBuilder.Entity<OrderStatus>(ConfigureOrderStatus);
|
2017-03-14 18:02:28 +01:00
|
|
|
|
modelBuilder.Entity<Buyer>(ConfigureBuyer);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 12:03:31 +01:00
|
|
|
|
private void ConfigureRequests(EntityTypeBuilder<ClientRequest> requestConfiguration)
|
|
|
|
|
{
|
|
|
|
|
requestConfiguration.ToTable("requests", DEFAULT_SCHEMA);
|
|
|
|
|
requestConfiguration.HasKey(cr => cr.Id);
|
|
|
|
|
requestConfiguration.Property(cr => cr.Name).IsRequired();
|
|
|
|
|
requestConfiguration.Property(cr => cr.Time).IsRequired();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-08 19:26:05 +01:00
|
|
|
|
|
2016-11-22 18:40:47 +01:00
|
|
|
|
void ConfigureBuyer(EntityTypeBuilder<Buyer> buyerConfiguration)
|
|
|
|
|
{
|
|
|
|
|
buyerConfiguration.ToTable("buyers", DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
|
|
buyerConfiguration.HasKey(b => b.Id);
|
|
|
|
|
|
2017-03-16 18:52:02 -07:00
|
|
|
|
buyerConfiguration.Ignore(b => b.DomainEvents);
|
2017-03-14 18:02:28 +01:00
|
|
|
|
|
2016-11-22 18:40:47 +01:00
|
|
|
|
buyerConfiguration.Property(b => b.Id)
|
|
|
|
|
.ForSqlServerUseSequenceHiLo("buyerseq", DEFAULT_SCHEMA);
|
|
|
|
|
|
2017-02-07 23:03:35 -08:00
|
|
|
|
buyerConfiguration.Property(b=>b.IdentityGuid)
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.HasMaxLength(200)
|
|
|
|
|
.IsRequired();
|
2016-11-24 14:59:25 +01:00
|
|
|
|
|
2017-02-07 23:03:35 -08:00
|
|
|
|
buyerConfiguration.HasIndex("IdentityGuid")
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.IsUnique(true);
|
|
|
|
|
|
2017-01-27 11:38:23 +01:00
|
|
|
|
buyerConfiguration.HasMany(b => b.PaymentMethods)
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.WithOne()
|
|
|
|
|
.HasForeignKey("BuyerId")
|
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
2017-01-27 11:38:23 +01:00
|
|
|
|
var navigation = buyerConfiguration.Metadata.FindNavigation(nameof(Buyer.PaymentMethods));
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 11:38:23 +01:00
|
|
|
|
void ConfigurePayment(EntityTypeBuilder<PaymentMethod> paymentConfiguration)
|
2016-11-22 18:40:47 +01:00
|
|
|
|
{
|
2017-01-27 11:38:23 +01:00
|
|
|
|
paymentConfiguration.ToTable("paymentmethods", DEFAULT_SCHEMA);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
|
|
|
|
paymentConfiguration.HasKey(b => b.Id);
|
|
|
|
|
|
2017-03-16 18:52:02 -07:00
|
|
|
|
paymentConfiguration.Ignore(b => b.DomainEvents);
|
2017-03-14 18:02:28 +01:00
|
|
|
|
|
2016-11-22 18:40:47 +01:00
|
|
|
|
paymentConfiguration.Property(b => b.Id)
|
|
|
|
|
.ForSqlServerUseSequenceHiLo("paymentseq", DEFAULT_SCHEMA);
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
paymentConfiguration.Property<int>("BuyerId")
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
paymentConfiguration.Property<string>("CardHolderName")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.HasMaxLength(200)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
paymentConfiguration.Property<string>("Alias")
|
2017-01-18 16:51:44 -08:00
|
|
|
|
.HasMaxLength(200)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
paymentConfiguration.Property<string>("CardNumber")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.HasMaxLength(25)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
paymentConfiguration.Property<DateTime>("Expiration")
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
paymentConfiguration.Property<int>("CardTypeId")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
paymentConfiguration.HasOne(p => p.CardType)
|
|
|
|
|
.WithMany()
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.HasForeignKey("CardTypeId");
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigureOrder(EntityTypeBuilder<Order> orderConfiguration)
|
|
|
|
|
{
|
|
|
|
|
orderConfiguration.ToTable("orders", DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
|
|
orderConfiguration.HasKey(o => o.Id);
|
|
|
|
|
|
2017-03-16 18:52:02 -07:00
|
|
|
|
orderConfiguration.Ignore(b => b.DomainEvents);
|
2017-03-14 18:02:28 +01:00
|
|
|
|
|
2016-11-22 18:40:47 +01:00
|
|
|
|
orderConfiguration.Property(o => o.Id)
|
|
|
|
|
.ForSqlServerUseSequenceHiLo("orderseq", DEFAULT_SCHEMA);
|
|
|
|
|
|
2017-07-13 12:34:19 +02:00
|
|
|
|
orderConfiguration.OwnsOne(o => o.Address);
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderConfiguration.Property<DateTime>("OrderDate").IsRequired();
|
2017-03-14 18:02:28 +01:00
|
|
|
|
orderConfiguration.Property<int?>("BuyerId").IsRequired(false);
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderConfiguration.Property<int>("OrderStatusId").IsRequired();
|
2017-03-14 18:02:28 +01:00
|
|
|
|
orderConfiguration.Property<int?>("PaymentMethodId").IsRequired(false);
|
2017-05-11 16:04:35 +02:00
|
|
|
|
orderConfiguration.Property<string>("Description").IsRequired(false);
|
2017-01-25 17:10:08 +01:00
|
|
|
|
|
|
|
|
|
var navigation = orderConfiguration.Metadata.FindNavigation(nameof(Order.OrderItems));
|
2017-02-02 13:34:44 -08:00
|
|
|
|
// DDD Patterns comment:
|
|
|
|
|
//Set as Field (New since EF 1.1) to access the OrderItem collection property through its field
|
2017-01-25 17:10:08 +01:00
|
|
|
|
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
2017-04-05 13:15:27 +02:00
|
|
|
|
orderConfiguration.HasOne<PaymentMethod>()
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.WithMany()
|
2017-01-27 11:38:23 +01:00
|
|
|
|
.HasForeignKey("PaymentMethodId")
|
2017-03-14 18:02:28 +01:00
|
|
|
|
.IsRequired(false)
|
2016-11-24 14:59:25 +01:00
|
|
|
|
.OnDelete(DeleteBehavior.Restrict);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
2017-04-03 11:32:35 +02:00
|
|
|
|
orderConfiguration.HasOne<Buyer>()
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.WithMany()
|
2017-03-14 18:02:28 +01:00
|
|
|
|
.IsRequired(false)
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.HasForeignKey("BuyerId");
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderConfiguration.HasOne(o => o.OrderStatus)
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.WithMany()
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.HasForeignKey("OrderStatusId");
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigureOrderItems(EntityTypeBuilder<OrderItem> orderItemConfiguration)
|
|
|
|
|
{
|
|
|
|
|
orderItemConfiguration.ToTable("orderItems", DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
|
|
orderItemConfiguration.HasKey(o => o.Id);
|
|
|
|
|
|
2017-03-16 18:52:02 -07:00
|
|
|
|
orderItemConfiguration.Ignore(b => b.DomainEvents);
|
2017-03-14 18:02:28 +01:00
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderItemConfiguration.Property(o => o.Id)
|
|
|
|
|
.ForSqlServerUseSequenceHiLo("orderitemseq");
|
|
|
|
|
|
|
|
|
|
orderItemConfiguration.Property<int>("OrderId")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderItemConfiguration.Property<decimal>("Discount")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderItemConfiguration.Property<int>("ProductId")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderItemConfiguration.Property<string>("ProductName")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderItemConfiguration.Property<decimal>("UnitPrice")
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
orderItemConfiguration.Property<int>("Units")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
2017-01-27 11:38:23 +01:00
|
|
|
|
|
|
|
|
|
orderItemConfiguration.Property<string>("PictureUrl")
|
|
|
|
|
.IsRequired(false);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
2016-11-24 14:59:25 +01:00
|
|
|
|
|
|
|
|
|
void ConfigureOrderStatus(EntityTypeBuilder<OrderStatus> orderStatusConfiguration)
|
|
|
|
|
{
|
|
|
|
|
orderStatusConfiguration.ToTable("orderstatus", DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
|
|
orderStatusConfiguration.HasKey(o => o.Id);
|
|
|
|
|
|
|
|
|
|
orderStatusConfiguration.Property(o => o.Id)
|
|
|
|
|
.HasDefaultValue(1)
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.ValueGeneratedNever()
|
2016-11-24 14:59:25 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
orderStatusConfiguration.Property(o => o.Name)
|
|
|
|
|
.HasMaxLength(200)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigureCardTypes(EntityTypeBuilder<CardType> cardTypesConfiguration)
|
|
|
|
|
{
|
|
|
|
|
cardTypesConfiguration.ToTable("cardtypes", DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
|
|
cardTypesConfiguration.HasKey(ct => ct.Id);
|
|
|
|
|
|
|
|
|
|
cardTypesConfiguration.Property(ct => ct.Id)
|
|
|
|
|
.HasDefaultValue(1)
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.ValueGeneratedNever()
|
2016-11-24 14:59:25 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
cardTypesConfiguration.Property(ct => ct.Name)
|
|
|
|
|
.HasMaxLength(200)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
}
|
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)
|
2017-05-08 13:59:59 +02:00
|
|
|
|
// performed throught the DbContext will be commited
|
2017-03-14 18:02:28 +01:00
|
|
|
|
var result = await base.SaveChangesAsync();
|
2017-03-16 18:52:02 -07:00
|
|
|
|
|
2017-03-27 14:05:28 +02:00
|
|
|
|
return true;
|
2017-03-14 18:02:28 +01:00
|
|
|
|
}
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
|
|
|
|
}
|