Browse Source

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"..
pull/49/merge
CESARDL 8 years ago
parent
commit
4708b89457
4 changed files with 37 additions and 14 deletions
  1. +1
    -1
      src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/Buyer.cs
  2. +27
    -7
      src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs
  3. +7
    -5
      src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderItem.cs
  4. +2
    -1
      src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs

+ 1
- 1
src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/Buyer.cs View File

@ -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() { }


+ 27
- 7
src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs View File

@ -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)


+ 7
- 5
src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderItem.cs View File

@ -6,16 +6,18 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
public class OrderItem
: Entity
{
private string _productName;
private string _pictureUrl;
private int _orderId;
// 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;
private decimal _unitPrice;
private decimal _discount;
private int _units;
private int _units;
public int ProductId { get; private set; }
protected OrderItem() { }
public OrderItem(int productId, string productName, decimal unitPrice, decimal discount, string PictureUrl, int units = 1)


+ 2
- 1
src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs View File

@ -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…
Cancel
Save