Aggregate Validations

Aggregate Validations

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


ifergus posted on Friday, May 18, 2007

What’s the best way to implement a validation rule when it involves an aggregation of multiple child objects?  For example, if I have a rule that states for a given order the total cost amount of the order items cannot exceed $5000, how would I do it?

In my implementation I have three classes as follows:

 

Order à OrderDetailCollection à OrderDetail

 

In the book (on page 454), I see that there is a similar example that considers all siblings in order to ensure that a particular Id is not reused.  However, it does not fit this need because the validation is not performed on the aggregate of the siblings. I'm using 2.1

RockfordLhotka replied on Friday, May 18, 2007

Typically you'd have your root object handle the ListChanged event of the child list. When an item changes in the child list, this event handler is notified and calls just one line of code:

ValidationRules.CheckRules("OrderTotal")

(assuming, of course, that OrderTotal is the name of the property you are using to display the total amount of the order)

Then all you need to do is write a rule method to ensure that the total amount doesn't exeed the maximum and connect that rule method to the OrderTotal property in AddBusinessRules().

Copyright (c) Marimer LLC