257 lines
9.2 KiB
C#
Raw Normal View History

2017-03-14 18:02:28 +01:00
using MediatR;
using Microsoft.EntityFrameworkCore;
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;
using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork;
2017-03-14 18:02:28 +01:00
using Ordering.Infrastructure;
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
{
const string DEFAULT_SCHEMA = "ordering";
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 OrderingContext(DbContextOptions options, IMediator mediator) : base(options)
{
_mediator = mediator;
}
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);
modelBuilder.Entity<Address>(ConfigureAddress);
modelBuilder.Entity<PaymentMethod>(ConfigurePayment);
2016-11-22 18:40:47 +01:00
modelBuilder.Entity<Order>(ConfigureOrder);
modelBuilder.Entity<OrderItem>(ConfigureOrderItems);
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();
}
void ConfigureAddress(EntityTypeBuilder<Address> addressConfiguration)
{
addressConfiguration.ToTable("address", DEFAULT_SCHEMA);
addressConfiguration.Property<int>("Id")
.IsRequired();
addressConfiguration.HasKey("Id");
}
2016-11-22 18:40:47 +01:00
void ConfigureBuyer(EntityTypeBuilder<Buyer> buyerConfiguration)
{
buyerConfiguration.ToTable("buyers", DEFAULT_SCHEMA);
buyerConfiguration.HasKey(b => b.Id);
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);
buyerConfiguration.Property(b=>b.IdentityGuid)
2016-11-22 18:40:47 +01:00
.HasMaxLength(200)
.IsRequired();
buyerConfiguration.HasIndex("IdentityGuid")
.IsUnique(true);
buyerConfiguration.HasMany(b => b.PaymentMethods)
.WithOne()
.HasForeignKey("BuyerId")
.OnDelete(DeleteBehavior.Cascade);
var navigation = buyerConfiguration.Metadata.FindNavigation(nameof(Buyer.PaymentMethods));
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
2016-11-22 18:40:47 +01:00
}
void ConfigurePayment(EntityTypeBuilder<PaymentMethod> paymentConfiguration)
2016-11-22 18:40:47 +01:00
{
paymentConfiguration.ToTable("paymentmethods", DEFAULT_SCHEMA);
2016-11-22 18:40:47 +01:00
paymentConfiguration.HasKey(b => b.Id);
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);
paymentConfiguration.Property<int>("BuyerId")
.IsRequired();
paymentConfiguration.Property<string>("CardHolderName")
2016-11-22 18:40:47 +01:00
.HasMaxLength(200)
.IsRequired();
paymentConfiguration.Property<string>("Alias")
.HasMaxLength(200)
.IsRequired();
paymentConfiguration.Property<string>("CardNumber")
2016-11-22 18:40:47 +01:00
.HasMaxLength(25)
.IsRequired();
paymentConfiguration.Property<DateTime>("Expiration")
.IsRequired();
paymentConfiguration.Property<int>("CardTypeId")
2016-11-22 18:40:47 +01:00
.IsRequired();
paymentConfiguration.HasOne(p => p.CardType)
.WithMany()
.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);
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);
orderConfiguration.Property<DateTime>("OrderDate").IsRequired();
2017-03-14 18:02:28 +01:00
orderConfiguration.Property<int?>("BuyerId").IsRequired(false);
orderConfiguration.Property<int>("OrderStatusId").IsRequired();
2017-03-14 18:02:28 +01:00
orderConfiguration.Property<int?>("PaymentMethodId").IsRequired(false);
var navigation = orderConfiguration.Metadata.FindNavigation(nameof(Order.OrderItems));
// DDD Patterns comment:
//Set as Field (New since EF 1.1) to access the OrderItem collection property through its field
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
2016-11-22 18:40:47 +01:00
orderConfiguration.HasOne<PaymentMethod>()
2016-11-22 18:40:47 +01:00
.WithMany()
.HasForeignKey("PaymentMethodId")
2017-03-14 18:02:28 +01:00
.IsRequired(false)
.OnDelete(DeleteBehavior.Restrict);
2016-11-22 18:40:47 +01:00
orderConfiguration.HasOne<Buyer>()
2016-11-22 18:40:47 +01:00
.WithMany()
2017-03-14 18:02:28 +01:00
.IsRequired(false)
.HasForeignKey("BuyerId");
2016-11-22 18:40:47 +01:00
orderConfiguration.HasOne(o => o.OrderStatus)
2016-11-22 18:40:47 +01:00
.WithMany()
.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);
orderItemConfiguration.Ignore(b => b.DomainEvents);
2017-03-14 18:02:28 +01:00
orderItemConfiguration.Property(o => o.Id)
.ForSqlServerUseSequenceHiLo("orderitemseq");
orderItemConfiguration.Property<int>("OrderId")
2016-11-22 18:40:47 +01:00
.IsRequired();
orderItemConfiguration.Property<decimal>("Discount")
2016-11-22 18:40:47 +01:00
.IsRequired();
orderItemConfiguration.Property<int>("ProductId")
2016-11-22 18:40:47 +01:00
.IsRequired();
orderItemConfiguration.Property<string>("ProductName")
2016-11-22 18:40:47 +01:00
.IsRequired();
orderItemConfiguration.Property<decimal>("UnitPrice")
.IsRequired();
orderItemConfiguration.Property<int>("Units")
2016-11-22 18:40:47 +01:00
.IsRequired();
orderItemConfiguration.Property<string>("PictureUrl")
.IsRequired(false);
2016-11-22 18:40:47 +01:00
}
void ConfigureOrderStatus(EntityTypeBuilder<OrderStatus> orderStatusConfiguration)
{
orderStatusConfiguration.ToTable("orderstatus", DEFAULT_SCHEMA);
orderStatusConfiguration.HasKey(o => o.Id);
orderStatusConfiguration.Property(o => o.Id)
.HasDefaultValue(1)
.ValueGeneratedNever()
.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)
.ValueGeneratedNever()
.IsRequired();
cardTypesConfiguration.Property(ct => ct.Name)
.HasMaxLength(200)
.IsRequired();
}
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 thought 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
}
}