@ -9,24 +9,41 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
public class Order
: Entity
{
// DDD Patterns comment
// Using private fields, allowed since EF Core 1.1, is a much better encapsulation
// aligned with DDD Aggregates and Domain Entities (Instead of properties and property collections)
private DateTime _orderDate ;
//TO DO: These fields need to be converted to a VALUE-OBJECT "Address"
private string _street ;
private string _city ;
private string _state ;
private string _country ;
private string _zipCode ;
private DateTime _orderDate ;
public Buyer Buyer { get ; private set ; }
int _buyerId ;
private int _buyerId ;
public OrderStatus OrderStatus { get ; private set ; }
int _orderStatusId ;
private int _orderStatusId ;
HashSet < OrderItem > _orderItems ;
public IEnumerable < OrderItem > OrderItems = > _orderItems . ToList ( ) . AsEnumerable ( ) ;
// DDD Patterns comment
// Using a private collection field, better for DDD Aggregate's encapsulation
// so OrderItems cannot be added from "outside the AggregateRoot" directly to the collection,
// but only through the method OrderAggrergateRoot.AddOrderItem() which includes behaviour.
private readonly HashSet < OrderItem > _orderItems ;
//TODO: Use List<> instead of HashSet<> because of the comment below
// So we won't need the ".ToList()"
public IEnumerable < OrderItem > OrderItems = > _orderItems . ToList ( ) . AsReadOnly ( ) ;
// Using List<>.AsReadOnly()
//This will create a read only wrapper around the private list.
//It's much cheaper than .ToList() because it will not have to copy all items in a new collection. (Just one heap alloc for the wrapper instance)
//https://msdn.microsoft.com/en-us/library/e78dcd75(v=vs.110).aspx
public PaymentMethod PaymentMethod { get ; private set ; }
int _paymentMethodId ;
private int _paymentMethodId ;
protected Order ( ) { }
@ -46,7 +63,10 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
_orderItems = new HashSet < OrderItem > ( ) ;
}
// DDD Patterns comment
// This Order AggregateRoot's method "AddOrderitem()" should be the only way to add Items to the Order,
// so any behavior (discounts, etc.) and validations are controlled by the AggregateRoot
// in order to maintain consistency between the whole Aggregate.
public void AddOrderItem ( int productId , string productName , decimal unitPrice , decimal discount , string pictureUrl , int units = 1 )
{
var existingOrderForProduct = _orderItems . Where ( o = > o . ProductId = = productId )