diff --git a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/BuyerEntityTypeConfiguration.cs b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/BuyerEntityTypeConfiguration.cs index f5bdea756..06ea7bcf6 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/BuyerEntityTypeConfiguration.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/BuyerEntityTypeConfiguration.cs @@ -1,41 +1,35 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; -using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.EntityConfigurations; -namespace Ordering.Infrastructure.EntityConfigurations +class BuyerEntityTypeConfiguration + : IEntityTypeConfiguration { - class BuyerEntityTypeConfiguration - : IEntityTypeConfiguration + public void Configure(EntityTypeBuilder buyerConfiguration) { - public void Configure(EntityTypeBuilder buyerConfiguration) - { - buyerConfiguration.ToTable("buyers", OrderingContext.DEFAULT_SCHEMA); + buyerConfiguration.ToTable("buyers", OrderingContext.DEFAULT_SCHEMA); - buyerConfiguration.HasKey(b => b.Id); + buyerConfiguration.HasKey(b => b.Id); - buyerConfiguration.Ignore(b => b.DomainEvents); + buyerConfiguration.Ignore(b => b.DomainEvents); - buyerConfiguration.Property(b => b.Id) - .UseHiLo("buyerseq", OrderingContext.DEFAULT_SCHEMA); + buyerConfiguration.Property(b => b.Id) + .UseHiLo("buyerseq", OrderingContext.DEFAULT_SCHEMA); - buyerConfiguration.Property(b => b.IdentityGuid) - .HasMaxLength(200) - .IsRequired(); + buyerConfiguration.Property(b => b.IdentityGuid) + .HasMaxLength(200) + .IsRequired(); - buyerConfiguration.HasIndex("IdentityGuid") - .IsUnique(true); + buyerConfiguration.HasIndex("IdentityGuid") + .IsUnique(true); - buyerConfiguration.Property(b => b.Name); + buyerConfiguration.Property(b => b.Name); - buyerConfiguration.HasMany(b => b.PaymentMethods) - .WithOne() - .HasForeignKey("BuyerId") - .OnDelete(DeleteBehavior.Cascade); + buyerConfiguration.HasMany(b => b.PaymentMethods) + .WithOne() + .HasForeignKey("BuyerId") + .OnDelete(DeleteBehavior.Cascade); - var navigation = buyerConfiguration.Metadata.FindNavigation(nameof(Buyer.PaymentMethods)); + var navigation = buyerConfiguration.Metadata.FindNavigation(nameof(Buyer.PaymentMethods)); - navigation.SetPropertyAccessMode(PropertyAccessMode.Field); - } + navigation.SetPropertyAccessMode(PropertyAccessMode.Field); } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/CardTypeEntityTypeConfiguration.cs b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/CardTypeEntityTypeConfiguration.cs index cdac780a1..c305c045b 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/CardTypeEntityTypeConfiguration.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/CardTypeEntityTypeConfiguration.cs @@ -1,27 +1,21 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; -using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.EntityConfigurations; -namespace Ordering.Infrastructure.EntityConfigurations +class CardTypeEntityTypeConfiguration + : IEntityTypeConfiguration { - class CardTypeEntityTypeConfiguration - : IEntityTypeConfiguration + public void Configure(EntityTypeBuilder cardTypesConfiguration) { - public void Configure(EntityTypeBuilder cardTypesConfiguration) - { - cardTypesConfiguration.ToTable("cardtypes", OrderingContext.DEFAULT_SCHEMA); + cardTypesConfiguration.ToTable("cardtypes", OrderingContext.DEFAULT_SCHEMA); - cardTypesConfiguration.HasKey(ct => ct.Id); + cardTypesConfiguration.HasKey(ct => ct.Id); - cardTypesConfiguration.Property(ct => ct.Id) - .HasDefaultValue(1) - .ValueGeneratedNever() - .IsRequired(); + cardTypesConfiguration.Property(ct => ct.Id) + .HasDefaultValue(1) + .ValueGeneratedNever() + .IsRequired(); - cardTypesConfiguration.Property(ct => ct.Name) - .HasMaxLength(200) - .IsRequired(); - } + cardTypesConfiguration.Property(ct => ct.Name) + .HasMaxLength(200) + .IsRequired(); } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/ClientRequestEntityTypeConfiguration.cs b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/ClientRequestEntityTypeConfiguration.cs index 5a5f8547e..17ee92e02 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/ClientRequestEntityTypeConfiguration.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/ClientRequestEntityTypeConfiguration.cs @@ -1,19 +1,13 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; -using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.EntityConfigurations; -namespace Ordering.Infrastructure.EntityConfigurations +class ClientRequestEntityTypeConfiguration + : IEntityTypeConfiguration { - class ClientRequestEntityTypeConfiguration - : IEntityTypeConfiguration + public void Configure(EntityTypeBuilder requestConfiguration) { - public void Configure(EntityTypeBuilder requestConfiguration) - { - requestConfiguration.ToTable("requests", OrderingContext.DEFAULT_SCHEMA); - requestConfiguration.HasKey(cr => cr.Id); - requestConfiguration.Property(cr => cr.Name).IsRequired(); - requestConfiguration.Property(cr => cr.Time).IsRequired(); - } + requestConfiguration.ToTable("requests", OrderingContext.DEFAULT_SCHEMA); + requestConfiguration.HasKey(cr => cr.Id); + requestConfiguration.Property(cr => cr.Name).IsRequired(); + requestConfiguration.Property(cr => cr.Time).IsRequired(); } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderEntityTypeConfiguration.cs b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderEntityTypeConfiguration.cs index 4e32763eb..e8fe7d97b 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderEntityTypeConfiguration.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderEntityTypeConfiguration.cs @@ -1,86 +1,78 @@ -using Microsoft.EntityFrameworkCore; -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.Infrastructure; -using System; +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.EntityConfigurations; -namespace Ordering.Infrastructure.EntityConfigurations +class OrderEntityTypeConfiguration : IEntityTypeConfiguration { - class OrderEntityTypeConfiguration : IEntityTypeConfiguration + public void Configure(EntityTypeBuilder orderConfiguration) { - public void Configure(EntityTypeBuilder orderConfiguration) - { - orderConfiguration.ToTable("orders", OrderingContext.DEFAULT_SCHEMA); + orderConfiguration.ToTable("orders", OrderingContext.DEFAULT_SCHEMA); - orderConfiguration.HasKey(o => o.Id); + orderConfiguration.HasKey(o => o.Id); - orderConfiguration.Ignore(b => b.DomainEvents); + orderConfiguration.Ignore(b => b.DomainEvents); - orderConfiguration.Property(o => o.Id) - .UseHiLo("orderseq", OrderingContext.DEFAULT_SCHEMA); + orderConfiguration.Property(o => o.Id) + .UseHiLo("orderseq", OrderingContext.DEFAULT_SCHEMA); - //Address value object persisted as owned entity type supported since EF Core 2.0 - orderConfiguration - .OwnsOne(o => o.Address, a => - { - // Explicit configuration of the shadow key property in the owned type - // as a workaround for a documented issue in EF Core 5: https://github.com/dotnet/efcore/issues/20740 - a.Property("OrderId") - .UseHiLo("orderseq", OrderingContext.DEFAULT_SCHEMA); - a.WithOwner(); - }); + //Address value object persisted as owned entity type supported since EF Core 2.0 + orderConfiguration + .OwnsOne(o => o.Address, a => + { + // Explicit configuration of the shadow key property in the owned type + // as a workaround for a documented issue in EF Core 5: https://github.com/dotnet/efcore/issues/20740 + a.Property("OrderId") + .UseHiLo("orderseq", OrderingContext.DEFAULT_SCHEMA); + a.WithOwner(); + }); - orderConfiguration - .Property("_buyerId") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("BuyerId") - .IsRequired(false); + orderConfiguration + .Property("_buyerId") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("BuyerId") + .IsRequired(false); - orderConfiguration - .Property("_orderDate") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("OrderDate") - .IsRequired(); + orderConfiguration + .Property("_orderDate") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("OrderDate") + .IsRequired(); - orderConfiguration - .Property("_orderStatusId") - // .HasField("_orderStatusId") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("OrderStatusId") - .IsRequired(); + orderConfiguration + .Property("_orderStatusId") + // .HasField("_orderStatusId") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("OrderStatusId") + .IsRequired(); - orderConfiguration - .Property("_paymentMethodId") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("PaymentMethodId") - .IsRequired(false); + orderConfiguration + .Property("_paymentMethodId") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("PaymentMethodId") + .IsRequired(false); - orderConfiguration.Property("Description").IsRequired(false); + orderConfiguration.Property("Description").IsRequired(false); - var navigation = orderConfiguration.Metadata.FindNavigation(nameof(Order.OrderItems)); + 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); + // DDD Patterns comment: + //Set as field (New since EF 1.1) to access the OrderItem collection property through its field + navigation.SetPropertyAccessMode(PropertyAccessMode.Field); - orderConfiguration.HasOne() - .WithMany() - // .HasForeignKey("PaymentMethodId") - .HasForeignKey("_paymentMethodId") - .IsRequired(false) - .OnDelete(DeleteBehavior.Restrict); + orderConfiguration.HasOne() + .WithMany() + // .HasForeignKey("PaymentMethodId") + .HasForeignKey("_paymentMethodId") + .IsRequired(false) + .OnDelete(DeleteBehavior.Restrict); - orderConfiguration.HasOne() - .WithMany() - .IsRequired(false) - // .HasForeignKey("BuyerId"); - .HasForeignKey("_buyerId"); + orderConfiguration.HasOne() + .WithMany() + .IsRequired(false) + // .HasForeignKey("BuyerId"); + .HasForeignKey("_buyerId"); - orderConfiguration.HasOne(o => o.OrderStatus) - .WithMany() - // .HasForeignKey("OrderStatusId"); - .HasForeignKey("_orderStatusId"); - } + orderConfiguration.HasOne(o => o.OrderStatus) + .WithMany() + // .HasForeignKey("OrderStatusId"); + .HasForeignKey("_orderStatusId"); } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderItemEntityTypeConfiguration.cs b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderItemEntityTypeConfiguration.cs index 9fd5734ed..04e2bbec5 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderItemEntityTypeConfiguration.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderItemEntityTypeConfiguration.cs @@ -1,59 +1,53 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; -using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.EntityConfigurations; -namespace Ordering.Infrastructure.EntityConfigurations +class OrderItemEntityTypeConfiguration + : IEntityTypeConfiguration { - class OrderItemEntityTypeConfiguration - : IEntityTypeConfiguration + public void Configure(EntityTypeBuilder orderItemConfiguration) { - public void Configure(EntityTypeBuilder orderItemConfiguration) - { - orderItemConfiguration.ToTable("orderItems", OrderingContext.DEFAULT_SCHEMA); - - orderItemConfiguration.HasKey(o => o.Id); - - orderItemConfiguration.Ignore(b => b.DomainEvents); - - orderItemConfiguration.Property(o => o.Id) - .UseHiLo("orderitemseq"); - - orderItemConfiguration.Property("OrderId") - .IsRequired(); - - orderItemConfiguration - .Property("_discount") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("Discount") - .IsRequired(); - - orderItemConfiguration.Property("ProductId") - .IsRequired(); - - orderItemConfiguration - .Property("_productName") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("ProductName") - .IsRequired(); - - orderItemConfiguration - .Property("_unitPrice") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("UnitPrice") - .IsRequired(); - - orderItemConfiguration - .Property("_units") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("Units") - .IsRequired(); - - orderItemConfiguration - .Property("_pictureUrl") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("PictureUrl") - .IsRequired(false); - } + orderItemConfiguration.ToTable("orderItems", OrderingContext.DEFAULT_SCHEMA); + + orderItemConfiguration.HasKey(o => o.Id); + + orderItemConfiguration.Ignore(b => b.DomainEvents); + + orderItemConfiguration.Property(o => o.Id) + .UseHiLo("orderitemseq"); + + orderItemConfiguration.Property("OrderId") + .IsRequired(); + + orderItemConfiguration + .Property("_discount") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("Discount") + .IsRequired(); + + orderItemConfiguration.Property("ProductId") + .IsRequired(); + + orderItemConfiguration + .Property("_productName") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("ProductName") + .IsRequired(); + + orderItemConfiguration + .Property("_unitPrice") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("UnitPrice") + .IsRequired(); + + orderItemConfiguration + .Property("_units") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("Units") + .IsRequired(); + + orderItemConfiguration + .Property("_pictureUrl") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("PictureUrl") + .IsRequired(false); } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderStatusEntityTypeConfiguration.cs b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderStatusEntityTypeConfiguration.cs index f968d9011..c99eb6bbe 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderStatusEntityTypeConfiguration.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/OrderStatusEntityTypeConfiguration.cs @@ -1,27 +1,21 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; -using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.EntityConfigurations; -namespace Ordering.Infrastructure.EntityConfigurations +class OrderStatusEntityTypeConfiguration + : IEntityTypeConfiguration { - class OrderStatusEntityTypeConfiguration - : IEntityTypeConfiguration + public void Configure(EntityTypeBuilder orderStatusConfiguration) { - public void Configure(EntityTypeBuilder orderStatusConfiguration) - { - orderStatusConfiguration.ToTable("orderstatus", OrderingContext.DEFAULT_SCHEMA); + orderStatusConfiguration.ToTable("orderstatus", OrderingContext.DEFAULT_SCHEMA); - orderStatusConfiguration.HasKey(o => o.Id); + orderStatusConfiguration.HasKey(o => o.Id); - orderStatusConfiguration.Property(o => o.Id) - .HasDefaultValue(1) - .ValueGeneratedNever() - .IsRequired(); + orderStatusConfiguration.Property(o => o.Id) + .HasDefaultValue(1) + .ValueGeneratedNever() + .IsRequired(); - orderStatusConfiguration.Property(o => o.Name) - .HasMaxLength(200) - .IsRequired(); - } + orderStatusConfiguration.Property(o => o.Name) + .HasMaxLength(200) + .IsRequired(); } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/PaymentMethodEntityTypeConfiguration.cs b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/PaymentMethodEntityTypeConfiguration.cs index 52fdf5f24..9083bcfcb 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/PaymentMethodEntityTypeConfiguration.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/EntityConfigurations/PaymentMethodEntityTypeConfiguration.cs @@ -1,65 +1,58 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; -using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; -using System; +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.EntityConfigurations; -namespace Ordering.Infrastructure.EntityConfigurations +class PaymentMethodEntityTypeConfiguration + : IEntityTypeConfiguration { - class PaymentMethodEntityTypeConfiguration - : IEntityTypeConfiguration + public void Configure(EntityTypeBuilder paymentConfiguration) { - public void Configure(EntityTypeBuilder paymentConfiguration) - { - paymentConfiguration.ToTable("paymentmethods", OrderingContext.DEFAULT_SCHEMA); - - paymentConfiguration.HasKey(b => b.Id); - - paymentConfiguration.Ignore(b => b.DomainEvents); - - paymentConfiguration.Property(b => b.Id) - .UseHiLo("paymentseq", OrderingContext.DEFAULT_SCHEMA); - - paymentConfiguration.Property("BuyerId") - .IsRequired(); - - paymentConfiguration - .Property("_cardHolderName") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("CardHolderName") - .HasMaxLength(200) - .IsRequired(); - - paymentConfiguration - .Property("_alias") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("Alias") - .HasMaxLength(200) - .IsRequired(); - - paymentConfiguration - .Property("_cardNumber") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("CardNumber") - .HasMaxLength(25) - .IsRequired(); - - paymentConfiguration - .Property("_expiration") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("Expiration") - .HasMaxLength(25) - .IsRequired(); - - paymentConfiguration - .Property("_cardTypeId") - .UsePropertyAccessMode(PropertyAccessMode.Field) - .HasColumnName("CardTypeId") - .IsRequired(); - - paymentConfiguration.HasOne(p => p.CardType) - .WithMany() - .HasForeignKey("_cardTypeId"); - } + paymentConfiguration.ToTable("paymentmethods", OrderingContext.DEFAULT_SCHEMA); + + paymentConfiguration.HasKey(b => b.Id); + + paymentConfiguration.Ignore(b => b.DomainEvents); + + paymentConfiguration.Property(b => b.Id) + .UseHiLo("paymentseq", OrderingContext.DEFAULT_SCHEMA); + + paymentConfiguration.Property("BuyerId") + .IsRequired(); + + paymentConfiguration + .Property("_cardHolderName") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("CardHolderName") + .HasMaxLength(200) + .IsRequired(); + + paymentConfiguration + .Property("_alias") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("Alias") + .HasMaxLength(200) + .IsRequired(); + + paymentConfiguration + .Property("_cardNumber") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("CardNumber") + .HasMaxLength(25) + .IsRequired(); + + paymentConfiguration + .Property("_expiration") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("Expiration") + .HasMaxLength(25) + .IsRequired(); + + paymentConfiguration + .Property("_cardTypeId") + .UsePropertyAccessMode(PropertyAccessMode.Field) + .HasColumnName("CardTypeId") + .IsRequired(); + + paymentConfiguration.HasOne(p => p.CardType) + .WithMany() + .HasForeignKey("_cardTypeId"); } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/Idempotency/ClientRequest.cs b/src/Services/Ordering/Ordering.Infrastructure/Idempotency/ClientRequest.cs index 7ca49fa41..96002ce1b 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/Idempotency/ClientRequest.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/Idempotency/ClientRequest.cs @@ -1,11 +1,8 @@ -using System; +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; -namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency +public class ClientRequest { - public class ClientRequest - { - public Guid Id { get; set; } - public string Name { get; set; } - public DateTime Time { get; set; } - } + public Guid Id { get; set; } + public string Name { get; set; } + public DateTime Time { get; set; } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/Idempotency/IRequestManager.cs b/src/Services/Ordering/Ordering.Infrastructure/Idempotency/IRequestManager.cs index d38c23e09..7adb31fe2 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/Idempotency/IRequestManager.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/Idempotency/IRequestManager.cs @@ -1,12 +1,8 @@ -using System; -using System.Threading.Tasks; +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; -namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency +public interface IRequestManager { - public interface IRequestManager - { - Task ExistAsync(Guid id); + Task ExistAsync(Guid id); - Task CreateRequestForCommandAsync(Guid id); - } + Task CreateRequestForCommandAsync(Guid id); } diff --git a/src/Services/Ordering/Ordering.Infrastructure/Idempotency/RequestManager.cs b/src/Services/Ordering/Ordering.Infrastructure/Idempotency/RequestManager.cs index f2ae2d876..bb053eeaf 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/Idempotency/RequestManager.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/Idempotency/RequestManager.cs @@ -1,43 +1,38 @@ -using Ordering.Domain.Exceptions; -using System; -using System.Threading.Tasks; +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; -namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency +public class RequestManager : IRequestManager { - public class RequestManager : IRequestManager - { - private readonly OrderingContext _context; + private readonly OrderingContext _context; - public RequestManager(OrderingContext context) - { - _context = context ?? throw new ArgumentNullException(nameof(context)); - } + public RequestManager(OrderingContext context) + { + _context = context ?? throw new ArgumentNullException(nameof(context)); + } - public async Task ExistAsync(Guid id) - { - var request = await _context. - FindAsync(id); + public async Task ExistAsync(Guid id) + { + var request = await _context. + FindAsync(id); - return request != null; - } + return request != null; + } - public async Task CreateRequestForCommandAsync(Guid id) - { - var exists = await ExistAsync(id); + public async Task CreateRequestForCommandAsync(Guid id) + { + var exists = await ExistAsync(id); - var request = exists ? - throw new OrderingDomainException($"Request with {id} already exists") : - new ClientRequest() - { - Id = id, - Name = typeof(T).Name, - Time = DateTime.UtcNow - }; + var request = exists ? + throw new OrderingDomainException($"Request with {id} already exists") : + new ClientRequest() + { + Id = id, + Name = typeof(T).Name, + Time = DateTime.UtcNow + }; - _context.Add(request); + _context.Add(request); - await _context.SaveChangesAsync(); - } + await _context.SaveChangesAsync(); } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs b/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs index f6e6d4a02..0ca4ef7cf 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs @@ -1,156 +1,141 @@ -using MediatR; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Design; -using Microsoft.EntityFrameworkCore.Storage; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork; -using Ordering.Infrastructure; -using Ordering.Infrastructure.EntityConfigurations; -using System; -using System.Data; -using System.Threading; -using System.Threading.Tasks; - -namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; + +public class OrderingContext : DbContext, IUnitOfWork { - 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) { - 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; } + _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); - private readonly IMediator _mediator; - private IDbContextTransaction _currentTransaction; - public OrderingContext(DbContextOptions options) : base(options) { } + System.Diagnostics.Debug.WriteLine("OrderingContext::ctor ->" + this.GetHashCode()); + } - public IDbContextTransaction GetCurrentTransaction() => _currentTransaction; + 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 bool HasActiveTransaction => _currentTransaction != null; + 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 OrderingContext(DbContextOptions options, IMediator mediator) : base(options) - { - _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + public async Task BeginTransactionAsync() + { + if (_currentTransaction != null) return null; + _currentTransaction = await Database.BeginTransactionAsync(IsolationLevel.ReadCommitted); - System.Diagnostics.Debug.WriteLine("OrderingContext::ctor ->" + this.GetHashCode()); - } + return _currentTransaction; + } - 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 CommitTransactionAsync(IDbContextTransaction transaction) + { + if (transaction == null) throw new ArgumentNullException(nameof(transaction)); + if (transaction != _currentTransaction) throw new InvalidOperationException($"Transaction {transaction.TransactionId} is not current"); - public async Task SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken)) + try { - // 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; + await SaveChangesAsync(); + transaction.Commit(); } - - public async Task BeginTransactionAsync() + catch { - if (_currentTransaction != null) return null; - - _currentTransaction = await Database.BeginTransactionAsync(IsolationLevel.ReadCommitted); - - return _currentTransaction; + RollbackTransaction(); + throw; } - - public async Task CommitTransactionAsync(IDbContextTransaction transaction) + finally { - if (transaction == null) throw new ArgumentNullException(nameof(transaction)); - if (transaction != _currentTransaction) throw new InvalidOperationException($"Transaction {transaction.TransactionId} is not current"); - - try - { - await SaveChangesAsync(); - transaction.Commit(); - } - catch - { - RollbackTransaction(); - throw; - } - finally + if (_currentTransaction != null) { - if (_currentTransaction != null) - { - _currentTransaction.Dispose(); - _currentTransaction = null; - } + _currentTransaction.Dispose(); + _currentTransaction = null; } } + } - public void RollbackTransaction() + public void RollbackTransaction() + { + try { - try - { - _currentTransaction?.Rollback(); - } - finally + _currentTransaction?.Rollback(); + } + finally + { + if (_currentTransaction != null) { - if (_currentTransaction != null) - { - _currentTransaction.Dispose(); - _currentTransaction = null; - } + _currentTransaction.Dispose(); + _currentTransaction = null; } } } +} - public class OrderingContextDesignFactory : IDesignTimeDbContextFactory +public class OrderingContextDesignFactory : IDesignTimeDbContextFactory +{ + public OrderingContext CreateDbContext(string[] args) { - public OrderingContext CreateDbContext(string[] args) - { - var optionsBuilder = new DbContextOptionsBuilder() - .UseSqlServer("Server=.;Initial Catalog=Microsoft.eShopOnContainers.Services.OrderingDb;Integrated Security=true"); + var optionsBuilder = new DbContextOptionsBuilder() + .UseSqlServer("Server=.;Initial Catalog=Microsoft.eShopOnContainers.Services.OrderingDb;Integrated Security=true"); - return new OrderingContext(optionsBuilder.Options, new NoMediator()); - } + return new OrderingContext(optionsBuilder.Options, new NoMediator()); + } - class NoMediator : IMediator + class NoMediator : IMediator + { + public Task Publish(TNotification notification, CancellationToken cancellationToken = default(CancellationToken)) where TNotification : INotification { - public Task Publish(TNotification notification, CancellationToken cancellationToken = default(CancellationToken)) where TNotification : INotification - { - return Task.CompletedTask; - } + return Task.CompletedTask; + } - public Task Publish(object notification, CancellationToken cancellationToken = default) - { - return Task.CompletedTask; - } + public Task Publish(object notification, CancellationToken cancellationToken = default) + { + return Task.CompletedTask; + } - public Task Send(IRequest request, CancellationToken cancellationToken = default(CancellationToken)) - { - return Task.FromResult(default(TResponse)); - } + public Task Send(IRequest request, CancellationToken cancellationToken = default(CancellationToken)) + { + return Task.FromResult(default(TResponse)); + } - public Task Send(object request, CancellationToken cancellationToken = default) - { - return Task.FromResult(default(object)); - } + public Task Send(object request, CancellationToken cancellationToken = default) + { + return Task.FromResult(default(object)); } } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/Repositories/BuyerRepository.cs b/src/Services/Ordering/Ordering.Infrastructure/Repositories/BuyerRepository.cs index 2e7b41bd6..615320842 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/Repositories/BuyerRepository.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/Repositories/BuyerRepository.cs @@ -1,68 +1,60 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork; -using System; -using System.Linq; -using System.Threading.Tasks; +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories; -namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories +public class BuyerRepository + : IBuyerRepository { - public class BuyerRepository - : IBuyerRepository + private readonly OrderingContext _context; + public IUnitOfWork UnitOfWork { - private readonly OrderingContext _context; - public IUnitOfWork UnitOfWork + get { - get - { - return _context; - } - } - - public BuyerRepository(OrderingContext context) - { - _context = context ?? throw new ArgumentNullException(nameof(context)); + return _context; } + } - public Buyer Add(Buyer buyer) - { - if (buyer.IsTransient()) - { - return _context.Buyers - .Add(buyer) - .Entity; - } - else - { - return buyer; - } - } + public BuyerRepository(OrderingContext context) + { + _context = context ?? throw new ArgumentNullException(nameof(context)); + } - public Buyer Update(Buyer buyer) + public Buyer Add(Buyer buyer) + { + if (buyer.IsTransient()) { return _context.Buyers - .Update(buyer) - .Entity; + .Add(buyer) + .Entity; } - - public async Task FindAsync(string identity) + else { - var buyer = await _context.Buyers - .Include(b => b.PaymentMethods) - .Where(b => b.IdentityGuid == identity) - .SingleOrDefaultAsync(); - return buyer; } + } - public async Task FindByIdAsync(string id) - { - var buyer = await _context.Buyers - .Include(b => b.PaymentMethods) - .Where(b => b.Id == int.Parse(id)) - .SingleOrDefaultAsync(); + public Buyer Update(Buyer buyer) + { + return _context.Buyers + .Update(buyer) + .Entity; + } - return buyer; - } + public async Task FindAsync(string identity) + { + var buyer = await _context.Buyers + .Include(b => b.PaymentMethods) + .Where(b => b.IdentityGuid == identity) + .SingleOrDefaultAsync(); + + return buyer; + } + + public async Task FindByIdAsync(string id) + { + var buyer = await _context.Buyers + .Include(b => b.PaymentMethods) + .Where(b => b.Id == int.Parse(id)) + .SingleOrDefaultAsync(); + + return buyer; } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs b/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs index 040256cf6..9040de569 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs @@ -1,63 +1,55 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork; -using System; -using System.Linq; -using System.Threading.Tasks; - -namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories +namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories; + +public class OrderRepository + : IOrderRepository { - public class OrderRepository - : IOrderRepository - { - private readonly OrderingContext _context; + private readonly OrderingContext _context; - public IUnitOfWork UnitOfWork + public IUnitOfWork UnitOfWork + { + get { - get - { - return _context; - } + return _context; } + } - public OrderRepository(OrderingContext context) - { - _context = context ?? throw new ArgumentNullException(nameof(context)); - } + public OrderRepository(OrderingContext context) + { + _context = context ?? throw new ArgumentNullException(nameof(context)); + } - public Order Add(Order order) - { - return _context.Orders.Add(order).Entity; + public Order Add(Order order) + { + return _context.Orders.Add(order).Entity; - } + } - public async Task GetAsync(int orderId) - { - var order = await _context - .Orders - .Include(x => x.Address) - .FirstOrDefaultAsync(o => o.Id == orderId); - if (order == null) - { - order = _context + public async Task GetAsync(int orderId) + { + var order = await _context .Orders - .Local - .FirstOrDefault(o => o.Id == orderId); - } - if (order != null) - { - await _context.Entry(order) - .Collection(i => i.OrderItems).LoadAsync(); - await _context.Entry(order) - .Reference(i => i.OrderStatus).LoadAsync(); - } - - return order; + .Include(x => x.Address) + .FirstOrDefaultAsync(o => o.Id == orderId); + if (order == null) + { + order = _context + .Orders + .Local + .FirstOrDefault(o => o.Id == orderId); } - - public void Update(Order order) + if (order != null) { - _context.Entry(order).State = EntityState.Modified; + await _context.Entry(order) + .Collection(i => i.OrderItems).LoadAsync(); + await _context.Entry(order) + .Reference(i => i.OrderStatus).LoadAsync(); } + + return order; + } + + public void Update(Order order) + { + _context.Entry(order).State = EntityState.Modified; } }