Unexpected behaviour with child IsValid properties

Unexpected behaviour with child IsValid properties

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


j055 posted on Tuesday, October 20, 2009


Hi

I'm using 3.7.1 with managed properties. I was expecting that when I call my root object's IsValid property it would always get all of the children and grandchildren IsValid properties. I guess there's a very good reason why it doesn't however I have the following problem:

In a child collection I have this override:

public override bool IsValid
{
get
{
// loop through PhoneNumbers
// CheckRules is internal and calls each child's ValidationRules.CheckRules
foreach (var item in this)
item.CheckRules();
return base.IsValid;
}
}

To ensure that this is called when the root IsValid is called I override the root as:

public override bool IsValid
{
get
{
return PhoneNumbers.IsValid && base.IsValid;
}
}

This seems rather fragile because there are likely to be other Collections that need checking in this way and this mod could easily be overlooked.

Can someone offer some thoughts or suggestions on how I might overcome this?
Thanks
Andrew

ajj3085 replied on Wednesday, October 21, 2009

If you're using a Managed Property to store you child collection in your root object, you shouldn't need to override IsValid.

In other words, it should function as you expect it.

j055 replied on Wednesday, October 21, 2009

In that case it doesn't work as expected:

PhoneNumbers.IsValid is as follows:


public override bool IsValid
{
get
{
foreach (var item in this)
item.CheckRules();
return base.IsValid;
}
}

Basically checks for duplicate numbers in the collection (similar to ProjectTracker Role NoDuplicates).

Without the Root IsValid override the object sometimes incorrectly shows a BrokenRule for the PhoneNumber when the root also contains a correct BrokenRule. I wonder if the child IsValid is only checked when the root IsValid is true?

Not really desired behaviour in my case.

ajj3085 replied on Wednesday, October 21, 2009

not sure running rules checks before calling isvalid is a great way to go. I'd remove that override, and override in the root object the childChanged, and if its something in your phone numbers collection, ask the collection to check its rules (which will ask each item in the collection to check is rules).

i do that, and IsValid works fine for me.

j055 replied on Thursday, October 22, 2009

OK, I think you're right.

I posted some code which I think does what you're suggesting a bit earlier:

http://forums.lhotka.net/forums/thread/37060.aspx

Cheers
Andrew

Copyright (c) Marimer LLC