Where to I validate that an Invoice has InvoiceItems?

Where to I validate that an Invoice has InvoiceItems?

Old forum URL: forums.lhotka.net/forums/t/803.aspx


cmay posted on Thursday, August 03, 2006

A pretty common example of what I am trying to do is an invoice with invoice items.

Invoice aggregates a collection of InvoiceItem objects.

If I understand, if I were to add a ValidationRule it would be on the property InvoiceItems, but this property is readonly obviously so PropertyChanged is never going to be called on this property.

Obviously I can just put a check in the UI code when the person clicks the save button, but I am betting there is another way to tap into the validation checking or the IsValid method on the business objects to make sure that a InvoiceItem collection of length 0 is invalid.

vargasbo replied on Thursday, August 03, 2006

If your invoiceItems is readonly, what changes would you have made to validate them? Now, you could have rules in your business object which states that only 4 invoice items can be in an invoice, in either case, those V.R. should be in parent object.

Mark replied on Thursday, August 03, 2006

You can always have the root Invoice object listen for the ListChanged event of the InvoiceItem collection and re-raise a PropertyChanged event.

Something like...

InvoiceItems.ListChanged += new System.ComponentModel.ListChangedEventHandler(ItemsChanged);

private void ItemsChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
{
   PropertyHasChanged("InvoiceItems");
}

private static bool InvoiceItemsRequired(object target, RuleArgs e)
{
   Invoice obj = target as Invoice;
   e.Description = "At least one item must be on file!";
   return (obj.InvoiceItems.Count > 0);
}

As long as you setup the validation rule to fire on "InvoiceItems", everything should work well...

Soumya replied on Tuesday, November 27, 2007

Can you tell where you have to write this 2 methods in Invoice class or InvoiceItems class.

Can you provide them in VB.NET

Soumya replied on Tuesday, November 27, 2007

never mind. I got answer.

I defined my list member with events and handling ListChanged in parent

Thanks

Soumya

Copyright (c) Marimer LLC