ValidatonRules And BusinessListBase

ValidatonRules And BusinessListBase

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


JoOfMetL posted on Thursday, October 19, 2006


Hello

I would like to know how to create rules which relate to a collection.
For example, a collection of object “Boat” which has a property “Name” and I wish to be able to check that “Name” is single.

Somebody with an idea?

Thank you


skagen00 replied on Thursday, October 19, 2006

If I understand you right you want to make sure the name of boats are all unique across the collection.

I have done tackled this sort of problem when it comes to "seasonal addresses". Someone can have a collection of addresses but the "month/date" ranges of seasonal addresses can't overlap.

Your case is much clearer so I'll just tell you how I'd tackle your problem.

I would first add a public get/internal set property on your boat named "IsBoatNameUnique". On a listchanged event of your collection, when a name is changed on a boat - or when boats are added or removed from the collection, you'd do a survey across the collection looking for name collisions. If you find collisions, the collection would communicate to their members via the internal set of the IsBoatNameUnique to tell whether or not the boat name is unique.

Then, on the boat, there would be a rule added - IsBoatNameValid, which would simply check this flag - if the name is not unique, broken rule, and the two "same named" boats would be invalid within the collection.

That is (at least if your problem is the same as my seasonal addresses entirely) how I'd probably tackle it. Though if anyone else has done this sort of thing differently I'd certainly be interested in hearing alternative solutions.

Fabio replied on Thursday, October 19, 2006

Another possible solution is override AddNewCore to prevent add of repeated obj.
Bo of the collection must override Equals and GetHashCode too.

Any way i think that is a great future to have BusinessRules for collection too.

ajj3085 replied on Thursday, October 19, 2006

I usually just override the IsValid property on the collection.  As I'm looping to see if each individual element is valid, I'm checking a dictionary to make sure the 'slot' isn't already filled with another object.  So the key to the dictionary in your case would be Name, and the value would be anything really.. just as long as key exists in the dictionary.

HTH
Andy

skagen00 replied on Thursday, October 19, 2006

Thanks Andy, that makes good sense. 

One nice thing to invalidating elements of the collection themselves is that if you have any number of them in a grid, you can highlight the "errant" ones and the databinding refresh is there, etc.

 

Fabio replied on Thursday, October 19, 2006

skagen00:

One nice thing to invalidating elements of the collection themselves is that if you have any number of them in a grid, you can highlight the "errant" ones and the databinding refresh is there, etc.



How many loop you have each time IsValid is invoked ?

skagen00 replied on Thursday, October 19, 2006

IsValid doesn't encumber any extra processing. It just knows there are children that are now invalid. The only time checking needs to be done is when an item is added/removed or an item's property of name changes. 

 

ajj3085 replied on Thursday, October 19, 2006

This is true.  The only downside is that the object needs to know about its parent... but I suppose it does anyway. 

Actually I'll have to see if I can retrofit a collection to do this, as the error on the line itself is a good idea.

JoOfMetL replied on Thursday, October 19, 2006

Thank you.

It is exactly my problem. Andy, your solution is good, but I would like that the user knows the reason of unvalidity of his collection.

Copyright (c) Marimer LLC