Click or drag to resize

Hierarchies

Hierarchies are used usually by repositories with hierarchical (self-reference) structure.

There are several important things to be remembered about hierarchies:

  • Hierarchies usually contain nodes, which are all from the same repository, but this is not always true.

    For example, the project type structure has its root elements to be project types, and then the structural elements comes below the project types.

  • Hierarchies can and should be preloaded, before accessing them in loops. See the PreloadHierarchy method of IHierarchy.

    PreloadHierarchy(ObjectTransaction)

Other than the above, most of the functionality of the hierarchies is self explanatory. For example, lets have the following tree:

  • 1

  • 1.1

  • 1.2

  • 1.2.1

  • 1.2.2

  • 1.3

  • 2

  • 2.1

  • 3

Each of the hierarchy methods with their return values are listed below:

  • GetRootNodes() => 1, 2, 3

  • GetDirectSubNodes(1) => 1.1, 1.2, 1.3

    GetDirectSubNodes(null) => 1, 2, 3

  • GetParentNode(1.1) => 1

    GetParentNode(1) => null

  • IsSubNodeOf(1.2.1, 1) => true

    IsSubNodeOf(1.2.1, null) => true

    IsSubNodeOf(1.2.1, 2) => false

  • TraverseAllSubNodes(1) => 1, 1.1, 1.2, 1.2.1, 1.2.2, 1.3 (includes the node itself)

    TraverseAllSubNodes(null) => 1, 1.1, 1.2, 1.2.1, 1.2.2, 1.3, 2, 2.1, 3 (the whole tree)

  • HasSubNodes(1) => true

    HasSubNodes(1.2) => true

    HasSubNodes(1.2.1) => false

    HasSubNodes(3) => false

    HasSubNodes(null) => true

  • GetTreeLevel(1) => 1

    GetTreeLevel(1.2) => 2

    GetTreeLevel(1.2.1) => 3

    GetTreeLevel(2) => 1

    GetTreeLevel(null) => 0

Example usage of hierarchy:

//preload the whole hierarchy for fast access
ProductGroupsRepository.Hierarchy.PreloadHierarchy(transaction);

foreach (var item in ProductGroupsRepository.Hierarchy.GetRootNodes(transaction))
{
  //do something for root groups
  ...
  foreach (var subItem in item.TraverseAllSubNodes()) //get all sub-groups deep
  {
    Console.WriteLine(subItem.GetFullCodePath());
  }
}

For more information, see the types: