Last active
August 22, 2018 04:10
-
-
Save arleypadua/d0b7ce1500d436b5e35b341b4b9b105b to your computer and use it in GitHub Desktop.
order-item-domain-model
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 OrderItem | |
{ | |
private OrderItem() { } | |
public static OrderItem New(string productId, int quantity, decimal price, decimal discount = 0) | |
{ | |
if (string.IsNullOrWhiteSpace(productId)) throw new ArgumentNullException(nameof(productId)); | |
if (quantity <= 0) throw new ArgumentOutOfRangeException(nameof(quantity)); | |
if (price <= 0) throw new ArgumentOutOfRangeException(nameof(price)); | |
if (discount < 0) throw new ArgumentOutOfRangeException(nameof(discount)); | |
if (discount > price * quantity) throw new ArgumentException("The discount cannot be more than the total of the order item.", nameof(discount)); | |
return new OrderItem | |
{ | |
ProductId = productId, | |
Quantity = quantity, | |
Price = price, | |
Discount = discount | |
}; | |
} | |
public string ProductId { get; private set; } | |
public int Quantity { get; private set; } | |
public decimal Price { get; private set; } | |
public decimal Discount { get; private set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment