Where I should Put this valiadtion rule ?

Where I should Put this valiadtion rule ?

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


AAKLebanon posted on Sunday, February 11, 2007

I have this problem: let's say that i have a collection of child items, for example an ArrivalSheet that is a collection of ArrivalDetail objects. an ArrivalDetail object represent the Startour and EndHour for a specific Employee(this is a payroll project...), and of course, the date of the arrival to the work.

so i have a tabl like this:

Date            StartHour      EndHour

1/1/2007      7 AM            4 PM

2/1/2007      7:30 AM        3 PM

...

now, i want to ensure that not 2 ArrivalDetail overides each other, for example, it is not acceptable to say that:

1/1/2007      7 AM      4 PM

1/1/2007      9 AM      2 PM

so, where i can put this validation rule ? i found that it is not good to put this rule in the ArrivalDetail class, because this will make this class responsible for the others in the collection.

I found that i must put this rule in the Collection class, that is in the ArivalSheet. but now the problem is, with what property should this rule be associated ? and when it will be cheked ?

 

I think this is a common problem in other domains also, for example, if a want to ensure that a Product Name is unique in a collection of Products, where I put this rule ?

 

d0tnetdude replied on Sunday, February 11, 2007

We've had to do something like this before, if the class doesn't have a parent then we do put the rule in the collection and perform the rule validation on the ListChanged event of the collection.  From what I remember this event will fire when any of the children are added, updated or deleted.

skagen00 replied on Monday, February 12, 2007

Exactly - do it on the list changed.

I have an almost identical situation to yours (mine has to do with date ranges overlapping) - on the listchanged, I cycle through the items as necessary to determine which ones overlap - I then have the collection set an internal setter to indicate on each child that overlaps that it overlaps. I tie a broken rule on each child to this property, so that when displaying a list of the children, it can be flagged which ones are invalid. That is, the ones that overlap.

 

AAKLebanon replied on Monday, February 12, 2007

Thinks for you, and I am interested in your approch,I will try to make the same think.

xal replied on Monday, February 12, 2007

I wrote something quite a while ago that handles this particular case and a few others. There are some complexities in writing something like this that may not be obvious at first sight, so it took a few revisions until I got it to a point where it was flexible, fast and responsive (and of course, yields the correct results).

I will try to write something on this subject if I find the time.

Andrés

Copyright (c) Marimer LLC