Partial refactoring about Order and OrderItems.
Set the fields to explicit private Using .ToReadOnly(), but still might need to use List<> instead HashSet<> so we won't need the .ToList() conversion which is "expensive"..
This commit is contained in:
parent
bf5af56d32
commit
4708b89457
@ -12,7 +12,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B
|
||||
|
||||
private HashSet<PaymentMethod> _paymentMethods;
|
||||
|
||||
public IEnumerable<PaymentMethod> PaymentMethods => _paymentMethods?.ToList().AsEnumerable();
|
||||
public IEnumerable<PaymentMethod> PaymentMethods => _paymentMethods?.ToList().AsReadOnly();
|
||||
|
||||
protected Buyer() { }
|
||||
|
||||
|
@ -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)
|
||||
|
@ -6,6 +6,9 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
public class OrderItem
|
||||
: 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 string _productName;
|
||||
private string _pictureUrl;
|
||||
private int _orderId;
|
||||
@ -15,7 +18,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
|
||||
public int ProductId { get; private set; }
|
||||
|
||||
|
||||
protected OrderItem() { }
|
||||
|
||||
public OrderItem(int productId, string productName, decimal unitPrice, decimal discount, string PictureUrl, int units = 1)
|
||||
|
@ -119,7 +119,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
|
||||
orderConfiguration.Property<int>("PaymentMethodId").IsRequired();
|
||||
|
||||
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);
|
||||
|
||||
orderConfiguration.HasOne(o => o.PaymentMethod)
|
||||
|
Loading…
x
Reference in New Issue
Block a user