The business layer in our application uses CSLA.NET. I've got business classes named Invoice and InvoiceItem (derived from a subclass of CSLA BusinessBase) and InvoiceItemList (derived from a subclass of CSLA BusinessBindingListBase).
Invoice class has a property for Items, declared as:
private static readonly PropertyInfo<InvoiceItemList> ItemsProperty =
RegisterProperty<InvoiceItemList>(o => o.Items, RelationshipTypes.Child);
public InvoiceItemList Items
{
get
{
return GetProperty(ItemsProperty);
}
private set
{
SetProperty(ItemsProperty, value);
}
}
I want to implement a CSLA Business Rule that ensures that Invoice is not saved without at least one item. So, I've created a MinCount rule, derived from CSLA PropertyRule and applied it to Items property.
BusinessRules.AddRule(new MinCount(ItemsProperty, 1));
The problem is, this rule is only fired when SetProperty is called on ItemsProperty, which is called only once (in DataPortal_Create). There are no items at the time of creation so the rule correctly marks the object as invalid. However the rule isn't fired when InvoiceItem objects are Added to/Removed from Items property. As a result the Invoice object remains invalid, and is not saved.
What is the correct way to implement rules (such as Min/Max Count rule) for properties that are derived from BusinessBindingListBase (or BusinessListBase)?