dotnet/eShop · error · OrderingDomainException
Invalid number of units
Error message
Invalid number of units
What it means
Thrown by the OrderItem constructor when units is less than or equal to zero. An OrderItem represents a positive quantity of a product on an order, so a non-positive unit count is an invalid invariant; the constructor enforces it as a precondition. It is an OrderingDomainException.
Source
Thrown at src/Ordering.Domain/AggregatesModel/OrderAggregate/OrderItem.cs:27
public string ProductName { get; private set; }
public string PictureUrl { get; private set;}
public decimal UnitPrice { get; private set;}
public decimal Discount { get; private set; }
public int Units { get; private set; }
public int ProductId { get; private set; }
protected OrderItem() { }
public OrderItem(int productId, string productName, decimal unitPrice, decimal discount, string pictureUrl, int units = 1)
{
if (units <= 0)
{
throw new OrderingDomainException("Invalid number of units");
}
if ((unitPrice * units) < discount)
{
throw new OrderingDomainException("The total of order item is lower than applied discount");
}
ProductId = productId;
ProductName = productName;
UnitPrice = unitPrice;
Discount = discount;
Units = units;
PictureUrl = pictureUrl;
}
public void SetNewDiscount(decimal discount)
{View on GitHub (pinned to 9b4f9434f4)
Solutions
- Validate at the application boundary that every order line has Quantity > 0 before mapping to OrderItem/ calling AddOrderItem.
- Filter out zero-or-negative quantity lines when converting a basket to an order.
- Use FluentValidation: RuleFor(x => x.Units).GreaterThan(0) on the create-order command.
- Trace the Quantity source to the basket/client payload and ensure it is always a positive integer.
Example fix
// before
order.AddOrderItem(p.Id, p.Name, p.Price, 0, p.PictureUrl, line.Quantity);
// after
if (line.Quantity <= 0) throw new ArgumentException("Quantity must be positive");
order.AddOrderItem(p.Id, p.Name, p.Price, 0, p.PictureUrl, line.Quantity); Defensive patterns
Strategy: validation
Validate before calling
if (units <= 0) throw new ArgumentOutOfRangeException(nameof(units)); order.AddOrderItem(productId, name, price, discount, url, units);
Type guard
static bool IsValidUnits(int units) => units > 0;
Try / catch
try {
order.AddOrderItem(id, name, price, discount, url, units);
} catch (OrderingDomainException ex) when (ex.Message == "Invalid number of units") {
// reject the order line as 400 / drop it from the batch
} Prevention
- Validate each order line Quantity > 0 before checkout.
- Drop zero/negative quantity lines when converting basket to order.
- Add FluentValidation GreaterThan(0) rules on create-order commands.
When it happens
Trigger: Constructing an OrderItem (directly or via Order.AddOrderItem, which news up an OrderItem for a new product line) with units <= 0. Typically the units value flowed from an order DTO/basket item whose Quantity was 0, negative, or an unset default.
Common situations: Basket item with Quantity 0 reaching the checkout handler; DTO-to-domain mapping that loses the Quantity field; a UI allowing zero-quantity lines; arithmetic that produced a negative units value (e.g. applying a delta).
Related errors
- The total of order item is lower than applied discount
- Discount is not valid
- Invalid units
- Item units desired should be greater than zero
- Is not possible to change the order status from {OrderStatus
AI-assisted analysis of dotnet/eShop@9b4f9434f4 (2026-08-13).
Data as JSON: /api/errors/0eb776189861fc2b.
Report an issue: GitHub.