-
Notifications
You must be signed in to change notification settings - Fork 25
Error Detection
Recall from the section Creating Your First Entity Tree how we created the root node:
var root = new ReadOnlyEntityTreeNode<int, Category>(c => c.CategoryId, rootItem);
In this constructor (and all subsequent node constructors we'll discuss) there's a third, optional parameter that we haven't mentioned until now. It's an enum of type ErrorCheckOptions, and it lets us define what errors will be detected as the tree is being built (and for mutable trees, when it is being modified).
ErrorCheckOptions elements are flags and can be combined. The flags are as follows:
-
None: No error checking -
CyclicIdDuplicates: detect cycles based on entity id -
SiblingIdDuplicates: detect duplicate sibling ids -
SiblingAliasDuplicates: detect duplicate sibling alias -
TreeScopeDuplicates: detect duplicate ids across the entire tree
Example:
var opt = ErrorCheckOptions.SiblingAliasDuplicates |
ErrorCheckOptions.CyclicIdDuplicates;
The default value is a combination of all but the last flag and is named Default.
So to re-write our tree node constructor with the default ErrorCheckOption made explicit, we would have:
var root = new ReadOnlyEntityTreeNode<int, Category>(c => c.CategoryId,
rootItem,
ErrorCheckOptions.Default);
Error detection with the
TreeScopeDuplicatesflag set causes a slightly less performant tree build because an internal hash map has to be maintained but is negligible for trees on the order of several thousand nodes.
A tree node's Error property indicates what error, if any, exists. This property is an enum of type IdentityError and corresponds closely to ErrorCheckOptions with the following flags:
NoneCyclicIdDuplicateSiblingIdDuplicateSiblingAliasDuplicateTreeScopeIdDuplicate
Example: to find all nodes in a tree with an error, we could do the following:
var errorNodes = root.Where(n => n.Error != IdentityError.None);
Example: to find all nodes with sibling alias errors, grouped by parent:
var errorNodes =
root
.Where(n => n.Error.HasFlag(IdentityError.SiblingAliasDuplicate)
.ToLookup(n => n.Parent.Id)