This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Order : Entity, IAggregateRoot | |
{ | |
// 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.SingleOrDefault(o => o.ProductId == productId); | |
if (existingOrderForProduct != null) | |
{ | |
//if previous line exist modify it with higher discount and units.. | |
if (discount > existingOrderForProduct.GetCurrentDiscount()) | |
{ | |
existingOrderForProduct.SetNewDiscount(discount); | |
} | |
existingOrderForProduct.AddUnits(units); | |
} | |
else | |
{ | |
//add validated new order item | |
var orderItem = new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units); | |
_orderItems.Add(orderItem); | |
} | |
} | |
public void SetStockConfirmedStatus() | |
{ | |
if (_orderStatusId == OrderStatus.AwaitingValidation.Id) | |
{ | |
AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id)); | |
_orderStatusId = OrderStatus.StockConfirmed.Id; | |
_description = "All the items were confirmed with available stock."; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment