What's the best way to make a parent valid that has an InValid child that is marked for deletion?

What's the best way to make a parent valid that has an InValid child that is marked for deletion?

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


JackMetcalfe posted on Wednesday, June 29, 2011

What is the best way to make a parent valid that has an InValid child that is marked for deletion?

I have a parent that has an optional child object object.  The child object is not valid causing the parent to not be valid.

Based on some criteria in the parent's OnChildChanged the child is marked for deletion (((IEditableBusinessObject)childObject).DeleteChild()).  However, the parent remains InValid after it is marked for deletion.

If I change the child object that is marked for deletion so that it becomes valid then it works but it seems this approach is like "spitting into the wind" and a better practice must exist.

The rule being broken on the child is that a required field is missing.  Another possible workaround is to create my own 'IsRequired' rule that checks if the object is marked for deletion (IsDeleted) and if it is then to not consider the rule broken.

Any suggestions?

Thanks in advance,

Jack

JonnyBee replied on Wednesday, June 29, 2011

Assuming that the child is a managed property - you should be able to just override IsValid on the child object to something like

    public override bool  IsValid
    {
	    get 
	    { 
               if (IsDeleted) return true;
               return base.IsValid;
	    }
    }

The IsValid property on the Parent will cascade down to it's children on managed properties.

JackMetcalfe replied on Wednesday, June 29, 2011

Thank you!  Simple and elegant.

Is the reason why IsValid does not already work this way because it is difficult to foresee all the possible scenarios?

Is there specific situations that you have encountered where this would not be the desired behavior.

Thanks again,

Jack

ajj3085 replied on Saturday, July 02, 2011

Ya IMO this should be a bug.  The user can't make the object valid once it's been deleted, which is a bad user experience.  I would expect that Csla then not consider IsValid for things in the deleted list. 

JonnyBee replied on Saturday, July 09, 2011

In this case/question there is a 1:1 realtionship (not involving a list) and then the FieldManager will cascade the IsValid to all children that implements ITrackStatus. For 1:n with a BusinessList child type this is handled correctly by Csla.

Copyright (c) Marimer LLC