public class Order : Entity
{
public Guid Id { get; private set; }
public CustomerId CustomerId { get; private set; }
public OrderStatus Status { get; private set; }
public List<OrderItem> Items { get; private set; }
public Money TotalAmount { get; private set; }
private Order() { } // For EF Core
public static Order Create(CustomerId customerId)
{
return new Order
{
Id = Guid.NewGuid(),
CustomerId = customerId,
Status = OrderStatus.Draft,
Items = new List<OrderItem>(),
TotalAmount = Money.Zero
};
}
public void AddItem(Product product, int quantity)
{
// Domain logic
if (Status != OrderStatus.Draft)
throw new DomainException("Cannot add items to confirmed order");
Items.Add(OrderItem.Create(product, quantity));
RecalculateTotal();
}
}
public record Money(decimal Amount, string Currency)
{
public static Money Zero => new(0, "USD");
public static Money operator +(Money a, Money b)
{
if (a.Currency != b.Currency)
throw new DomainException("Cannot add different currencies");
return new Money(a.Amount + b.Amount, a.Currency);
}
}
public record Address(
string Street,
string City,
string State,
string ZipCode,
string Country)
{
public string FullAddress => $"{Street}, {City}, {State} {ZipCode}, {Country}";
}
// Write Model (Command)
public class CreateOrderCommand
{
public List<CreateOrderItemCommand> Items { get; set; }
public Guid CustomerId { get; set; }
}
// Read Model (Query)
public class OrderSummaryDto
{
public Guid Id { get; set; }
public string CustomerName { get; set; }
public int ItemCount { get; set; }
public decimal TotalAmount { get; set; }
public string Status { get; set; }
}