Validation rules dependant on other items in collection

Validation rules dependant on other items in collection

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


j055 posted on Friday, July 17, 2009

Hi

I have a rule which checks for duplicates in the collection. If the Web UI changes another item in the collection so it no longer contains duplicates then the original item still has it's broken rule. How can I reset or remove the original item's broken rule and at what point in the cycle?

Many thanks
Andrew

skagen00 replied on Friday, July 17, 2009

What I have tended to do is make the parent responsible for duplicate broken rule issues with children.

Capturing a listchanged (or child changed) event for any given child, I have the parent scan over the collection for duplicates.

The parent sets a property on each child through an internal setter - "IsAddressUnique". Each child has a broken rule that basically is broken if IsAddressUnique is false.

Chris

j055 replied on Friday, July 17, 2009

OK, That makes sense I think. I'll give it a go. How do I deal with items being removed. My item may become valid if another is removed but I'm not sure the ListChanged event or ChildChanged event can alert the parent. Those events just fire when properties change don't they?

Thanks again
Andrew

skagen00 replied on Friday, July 17, 2009

Nope, you'll get notifications for deletions too.

j055 replied on Friday, July 17, 2009

Hi

Add this to my BLB. Fires on property changes but not a remove from collection.

protected override void OnChildChanged(Csla.Core.ChildChangedEventArgs e)
{
Debug.WriteLine("ChildChanged");
}

I'm using v3.6.2.0. What am I missing?
Thanks
Andrew

skagen00 replied on Friday, July 17, 2009

I'm guessing the BLB doesn't detect that - a child didn't change in this respect, it did.

Try adding your override to the business base containing the business list base.

Vinodonly replied on Monday, July 20, 2009

I had a req where a total percentage entered in child objects should be exactly 100, to achieve that I have added helpers in both parent and child objects.

Although the normal rule checking triggers the routine and makes the individual obj valid/invalid.

But suppose there are 2 child objects in which user entered 40 and 60 percent. later on he went and changed 60 to 55 which made that obj invalid but then he went and changed 40 to 45. At this point, complete collection is valid.

So to acheive this, Here is the complete flow on how I made it :-

1. After the PropertySetter in child bo, make a call to a Helper which triggers the parent checking routine. here is that method

void TriggerAllRulesChk()
{
FabAccETDs _FabAccETDs = this.Parent as FabAccETDs;
if (!_FabAccETDs.XIsEmpty())
_FabAccETDs.ChkRules();
}

Parent collection is having following method, this simply loops through all items and re-checks rules

internal void ChkRules()
{
foreach (var item in this)
{
item.ChkRules();
}

}

coming back to the child, bcos ValidationRules.CheckRules() can only be called from within class, I have added this helper..

internal void ChkRules()
{ ValidationRules.CheckRules(); }

Pls let me know if this helps or if this approach is not good and there is a better option avl..

Vinodonly replied on Monday, July 20, 2009

Hi Rocky,

Even in tracker app, in role checking it will fail to validate without the extra handling mentioned in this post.

Kindly advise your comments.

j055 replied on Tuesday, July 21, 2009

Hi

For my duplicates problem I ended up doing the following, expose BO checkrules:

internal void CheckRules()
{
ValidationRules.CheckRules();
}

Then in BLB:

protected override void OnChildChanged(ChildChangedEventArgs e)
{
var target = (MyObject) e.ChildObject;
CheckChildrenRules(target);
base.OnChildChanged(e);
}

private void CheckChildrenRules(MyObject target)
{
foreach (var item in this)
if (!(ReferenceEquals(item, target)))
item.CheckRules();
}

And this in case a removed item makes the BLB valid:

protected override void RemoveItem(int index)
{
var item = this[index];
base.RemoveItem(index);
CheckChildrenRules(item);
}

I found that setting a child property from within OnChildChanged was dangerous because that in turn fires the ChildChanged event, potentially raising a stack overflow exception.

Thanks for all the help.
Andrew

Vinodonly replied on Wednesday, July 29, 2009

What is the recommended approach in this case... Even my method which I mentioned before was not triggering validations if item was deleted from list..

PTTracker will also have same issue without this extra handling..

After experimenting with other events i.e. childchanged, removingitem etc. I finally found that ListChanged event is triggering the validations when item is removed and that is a easier approach also..

I just wanted to know if this is the recommended approach...

Copyright (c) Marimer LLC