BrokenRulesCollection for Root and Children, ISupportBrokenRulesBrokenRulesCollection for Root and Children, ISupportBrokenRules
Old forum URL: forums.lhotka.net/forums/t/5532.aspx
JeroenEikmans posted on Saturday, October 04, 2008
Hi,
I wanted to create a 'generic' way of accessing the BrokenRulesCollection of an editable root and all of it's children. I'm overriding the BrokenRulesCollection of the root. This is roughly the idea:
public override BrokenRulesCollection BrokenRulesCollection
{
get
{
BrokenRulesCollection brokenRules = BrokenRulesCollection.CreateCollection();
// merge the brokenrules of this
brokenRules.Merge("??", base.BrokenRulesCollection);
// merge the brokenrules of the children
List<object> children = this.FieldManager.GetChildren();
foreach (object child in children)
{
// child could be an editable object
if (child is IEditableBusinessObject)
{
ISupportBrokenRules editableBaseEntity = child as ISupportBrokenRules;
if (editableBaseEntity != null)
// recursive call to BrokenRulesCollection
brokenRules.Merge("??", editableBaseEntity.BrokenRulesCollection);
}
// child could be an editable collection
if (child is IEditableCollection)
{
IList list = child as IList;
for (int index = 0; index < list.Count; index++)
{
ISupportBrokenRules editableBaseEntity = list[index] as ISupportBrokenRules;
if (editableBaseEntity != null)
// recursive call to BrokenRulesCollection
brokenRules.Merge("{0}.{1}[{2}]".FormatWith("??", "??", index), editableBaseEntity.BrokenRulesCollection);
}
}
}
return brokenRules;
}
}
You see there is a cast to ISupportBrokenRules because the GetChildren returns a list of objects. The definition is:
public interface ISupportBrokenRules : ITrackStatus
{
BrokenRulesCollection BrokenRulesCollection { get; }
}
It would be nice to have the ISupportBrokenRules interface defined by the CSLA framework and not by me. Or is there an interface that already does this?
Maybe there's a smarter way to achieve what I'm doing here alltogether. Any opinions?
thanks,
Jeroen
JoeFallon1 replied on Saturday, October 04, 2008
I have posted a complete working solution to this before:
http://forums.lhotka.net/forums/post/7444.aspx
It may be a bit out of date now that CSLA has added so much extensibility. But you can review it for ideas.
HTH
Joe
JeroenEikmans replied on Sunday, October 05, 2008
Thanks for your link to the other post. Your solution builds a complete BrokenRulesCollection for all invalid children, including the ones in editable collections. That's nice, I can use that.
For now I only want to build a BrokenRulesCollection that is a 'result' of a call to the IsValid property of the root, so including only the invalid children and the first invalid children in editable collections.
When the root is not valid, I just want to check the BrokenRulesCollection of the root to see why the root is not valid.
thanks,
Jeroen
FrankM replied on Tuesday, October 14, 2008
How about
foreach ( var child in children)
{
if (child is BusinessBase)
{
brokenRules.Merge("??", ((BusinessBase)child).BrokenRulesCollection);
}
}
I am doing the similar stuff.
Copyright (c) Marimer LLC