Skip to content

Error Detection

David West edited this page Aug 26, 2016 · 20 revisions

< Entity Aliases

Detection Options

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 TreeScopeDuplicates flag 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.

Assessing Errors

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:

  • None
  • CyclicIdDuplicate
  • SiblingIdDuplicate
  • SiblingAliasDuplicate
  • TreeScopeIdDuplicate

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)

Mutable Trees >

Clone this wiki locally