How to automatically check the same property with different values

How to automatically check the same property with different values

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


fanste posted on Monday, July 29, 2013

Hi,

I have some problem...I have gridview and one column Name, so I implemented Business rule the Name is required and rule the Name is uniqe ...so when I entered text in first row example "Car" after that I entred the same text in second row the rule will be invoked and I got the error...That's fine...but problem is when I change first row for example entered "Car1" the error in second row still there... 

Please help,

Stefan

skagen00 replied on Monday, July 29, 2013

One sort of avenue for a rule like this is to have a managed property that has an internal setter on the child for "IsNameUnique" (I'm assuming you're checking unique amongst children in a collection).  Then, simply have a business rule tied to this managed property on the child class to validate that IsNameUnique = true.  Literally, that's the rule - make sure the value is true.

Then, in OnChildChanged of the collection when the propertychangedargs says the name changed on a child, just do a quick traversal of children to recheck uniqueness rules on an o(n) manner, setting this IsNameUnique value for all children.  I would wager this sort of algorithm would work in a performantly acceptable way for a hundred+ children.

Just one avenue.

fanste replied on Tuesday, July 30, 2013

Hi,

that is look fine to me. But how to call CheckRules in Collection class ?

Thanks

cfarren replied on Tuesday, July 30, 2013

You can't. You have to loop through each item in the collection and call CheckRules on the item class.

fanste replied on Tuesday, July 30, 2013

It's works. Thanks

JonnyBee replied on Tuesday, July 30, 2013

One might argue that the responsibility for a unique entry should be handled at a higher level (a BusinessObject that owns the list) rather than within each child.

So you may also want to look at the NoDuplicates generic rule that was posted earlier to validate the list as such:
http://forums.lhotka.net/forums/p/10494/49113.aspx#49113

skagen00 replied on Tuesday, July 30, 2013

I think it's true what Johnny says, but if you want the error indicators to nicely show next to each offending child item (which seems to have a lot of value in cases like this), it can't easily be done from the parent object.

 

JonnyBee replied on Tuesday, July 30, 2013

Yes, it has to do with the user experience as well. 

It would be very easy f.ex to have button and action for "Show Duplicates" and use a FilteredBindingList / LINQ query to show and order the duplicate items on the unique key. 

Calling rules / setting a property at runtime is a valid approach - just be aware of possible dependencies and how many rules you _ACTUALLY_ end up running (and how many OnPropertyChanged event is atually generated to notify the UI), especially if it is a long list and the rule must also make sure to be valid when the Parent list is null (ex new object - not yet added to the list).

 

cfarren replied on Monday, July 29, 2013

What is the structure of the business objects? Do you have a parent of type BusinessBase that has a child list class (BusinessListBase) which is used to populate the grid or is it just a BusinessListBase?

If your parent object is a BusinessBase then inside the class that determines the lines in the grid, set the Name properties PropertyInfo backing field to be internal or friend and then override OnChildChanged in the parent class. When that is called you can loop through each of the items in the BusinessListBase and call CheckRules on the backing field.

Alternatively you can create a rule in the line object that loops though each item in its parent list and calls CheckRules.

My apologies if that doesn't make sense. My morning coffee hasn't kicked in yet Wink

fanste replied on Tuesday, July 30, 2013

Hi,

I have BusinessBase class and BusinessBindingListBase, grid is binding with BusinessBindingListBase. 

Copyright (c) Marimer LLC