This commit is contained in:
Ramón Tomás 2017-11-08 16:28:21 +01:00
commit 493e12dc98
7 changed files with 19 additions and 40 deletions

View File

@ -4,17 +4,12 @@ using System.Collections.Generic;
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate
{ {
public class Address public class Address : ValueObject
:ValueObject
{ {
public String Street { get; private set; } public String Street { get; private set; }
public String City { get; private set; } public String City { get; private set; }
public String State { get; private set; } public String State { get; private set; }
public String Country { get; private set; } public String Country { get; private set; }
public String ZipCode { get; private set; } public String ZipCode { get; private set; }
private Address() { } private Address() { }
@ -30,6 +25,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
protected override IEnumerable<object> GetAtomicValues() protected override IEnumerable<object> GetAtomicValues()
{ {
// Using a yield return statement to return each element one at a time
yield return Street; yield return Street;
yield return City; yield return City;
yield return State; yield return State;

View File

@ -15,6 +15,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
// aligned with DDD Aggregates and Domain Entities (Instead of properties and property collections) // aligned with DDD Aggregates and Domain Entities (Instead of properties and property collections)
private DateTime _orderDate; private DateTime _orderDate;
// Address is a Value Object pattern example persisted as EF Core 2.0 owned entity
public Address Address { get; private set; } public Address Address { get; private set; }
private int? _buyerId; private int? _buyerId;
@ -177,16 +178,16 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string cardNumber, private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string cardNumber,
string cardSecurityNumber, string cardHolderName, DateTime cardExpiration) string cardSecurityNumber, string cardHolderName, DateTime cardExpiration)
{ {
var orderStartedDomainEvent = new OrderStartedDomainEvent( var orderStartedDomainEvent = new OrderStartedDomainEvent(this, userId, cardTypeId,
this, userId, cardTypeId, cardNumber, cardSecurityNumber, cardNumber, cardSecurityNumber,
cardHolderName, cardExpiration); cardHolderName, cardExpiration);
this.AddDomainEvent(orderStartedDomainEvent); this.AddDomainEvent(orderStartedDomainEvent);
} }
private void StatusChangeException(OrderStatus orderStatusToChange) private void StatusChangeException(OrderStatus orderStatusToChange)
{ {
throw new OrderingDomainException($"Not possible to change order status from {OrderStatus.Name} to {orderStatusToChange.Name}."); throw new OrderingDomainException($"Is not possible to change the order status from {OrderStatus.Name} to {orderStatusToChange.Name}.");
} }
public decimal GetTotal() public decimal GetTotal()

View File

@ -9,8 +9,7 @@ namespace Ordering.Domain.Events
/// <summary> /// <summary>
/// Event used when an order is created /// Event used when an order is created
/// </summary> /// </summary>
public class OrderStartedDomainEvent public class OrderStartedDomainEvent : INotification
: INotification
{ {
public string UserId { get; private set; } public string UserId { get; private set; }
public int CardTypeId { get; private set; } public int CardTypeId { get; private set; }
@ -21,9 +20,9 @@ namespace Ordering.Domain.Events
public Order Order { get; private set; } public Order Order { get; private set; }
public OrderStartedDomainEvent(Order order, string userId, public OrderStartedDomainEvent(Order order, string userId,
int cardTypeId, string cardNumber, int cardTypeId, string cardNumber,
string cardSecurityNumber, string cardHolderName, string cardSecurityNumber, string cardHolderName,
DateTime cardExpiration) DateTime cardExpiration)
{ {
Order = order; Order = order;
UserId = userId; UserId = userId;

View File

@ -6,12 +6,8 @@
public abstract class Entity public abstract class Entity
{ {
int? _requestedHashCode; int? _requestedHashCode;
int _Id; int _Id;
private List<INotification> _domainEvents;
public virtual int Id public virtual int Id
{ {
get get
@ -24,13 +20,14 @@
} }
} }
public List<INotification> DomainEvents => _domainEvents; private List<INotification> _domainEvents;
public List<INotification> DomainEvents => _domainEvents;
public void AddDomainEvent(INotification eventItem) public void AddDomainEvent(INotification eventItem)
{ {
_domainEvents = _domainEvents ?? new List<INotification>(); _domainEvents = _domainEvents ?? new List<INotification>();
_domainEvents.Add(eventItem); _domainEvents.Add(eventItem);
} }
public void RemoveDomainEvent(INotification eventItem) public void RemoveDomainEvent(INotification eventItem)
{ {
if (_domainEvents is null) return; if (_domainEvents is null) return;
@ -74,7 +71,6 @@
return base.GetHashCode(); return base.GetHashCode();
} }
public static bool operator ==(Entity left, Entity right) public static bool operator ==(Entity left, Entity right)
{ {
if (Object.Equals(left, null)) if (Object.Equals(left, null))

View File

@ -14,16 +14,13 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
return ReferenceEquals(left, null) || left.Equals(right); return ReferenceEquals(left, null) || left.Equals(right);
} }
protected static bool NotEqualOperator(ValueObject left, ValueObject right) protected static bool NotEqualOperator(ValueObject left, ValueObject right)
{ {
return !(EqualOperator(left, right)); return !(EqualOperator(left, right));
} }
protected abstract IEnumerable<object> GetAtomicValues(); protected abstract IEnumerable<object> GetAtomicValues();
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null || obj.GetType() != GetType()) if (obj == null || obj.GetType() != GetType())
@ -47,7 +44,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
return !thisValues.MoveNext() && !otherValues.MoveNext(); return !thisValues.MoveNext() && !otherValues.MoveNext();
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return GetAtomicValues() return GetAtomicValues()

View File

@ -7,8 +7,7 @@ using System;
namespace Ordering.Infrastructure.EntityConfigurations namespace Ordering.Infrastructure.EntityConfigurations
{ {
class OrderEntityTypeConfiguration class OrderEntityTypeConfiguration : IEntityTypeConfiguration<Order>
: IEntityTypeConfiguration<Order>
{ {
public void Configure(EntityTypeBuilder<Order> orderConfiguration) public void Configure(EntityTypeBuilder<Order> orderConfiguration)
{ {
@ -21,6 +20,7 @@ namespace Ordering.Infrastructure.EntityConfigurations
orderConfiguration.Property(o => o.Id) orderConfiguration.Property(o => o.Id)
.ForSqlServerUseSequenceHiLo("orderseq", OrderingContext.DEFAULT_SCHEMA); .ForSqlServerUseSequenceHiLo("orderseq", OrderingContext.DEFAULT_SCHEMA);
//Address value object persisted as owned entity type supported since EF Core 2.0
orderConfiguration.OwnsOne(o => o.Address); orderConfiguration.OwnsOne(o => o.Address);
orderConfiguration.Property<DateTime>("OrderDate").IsRequired(); orderConfiguration.Property<DateTime>("OrderDate").IsRequired();
@ -32,7 +32,7 @@ namespace Ordering.Infrastructure.EntityConfigurations
var navigation = orderConfiguration.Metadata.FindNavigation(nameof(Order.OrderItems)); var navigation = orderConfiguration.Metadata.FindNavigation(nameof(Order.OrderItems));
// DDD Patterns comment: // DDD Patterns comment:
//Set as Field (New since EF 1.1) to access the OrderItem collection property through its field //Set as field (New since EF 1.1) to access the OrderItem collection property through its field
navigation.SetPropertyAccessMode(PropertyAccessMode.Field); navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
orderConfiguration.HasOne<PaymentMethod>() orderConfiguration.HasOne<PaymentMethod>()

View File

@ -12,22 +12,14 @@ using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
{ {
public class OrderingContext public class OrderingContext : DbContext, IUnitOfWork
: DbContext,IUnitOfWork
{ {
public const string DEFAULT_SCHEMA = "ordering"; public const string DEFAULT_SCHEMA = "ordering";
public DbSet<Order> Orders { get; set; } public DbSet<Order> Orders { get; set; }
public DbSet<OrderItem> OrderItems { get; set; } public DbSet<OrderItem> OrderItems { get; set; }
public DbSet<PaymentMethod> Payments { get; set; } public DbSet<PaymentMethod> Payments { get; set; }
public DbSet<Buyer> Buyers { get; set; } public DbSet<Buyer> Buyers { get; set; }
public DbSet<CardType> CardTypes { get; set; } public DbSet<CardType> CardTypes { get; set; }
public DbSet<OrderStatus> OrderStatus { get; set; } public DbSet<OrderStatus> OrderStatus { get; set; }
private readonly IMediator _mediator; private readonly IMediator _mediator;
@ -63,7 +55,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
// You will need to handle eventual consistency and compensatory actions in case of failures in any of the Handlers. // You will need to handle eventual consistency and compensatory actions in case of failures in any of the Handlers.
await _mediator.DispatchDomainEventsAsync(this); await _mediator.DispatchDomainEventsAsync(this);
// After executing this line all the changes (from the Command Handler and Domain Event Handlers) // After executing this line all the changes (from the Command Handler and Domain Event Handlers)
// performed throught the DbContext will be commited // performed throught the DbContext will be commited
var result = await base.SaveChangesAsync(); var result = await base.SaveChangesAsync();