Skip to content

Extensibility

lordmilko edited this page Sep 11, 2021 · 4 revisions

Contents

Overview

For scenarios in which you need to perform advanced manipulations/analytics, custom visitor types can be defined. PrtgAPI defines several base types you can leverage in designing your visitor:

Visitor Result Base Description
PrtgNodeVisitor void Visits a single PrtgNode. Fully abstract class.
PrtgNodeVisitor<T> T Visits a single PrtgNode and returns a value of type T. Fully abstract class
PrtgNodeWalker void PrtgNodeVisitor Traverses every node of a PrtgNode tree
PrtgNodeRewriter PrtgNode PrtgNodeVisitor<T> Traverses every node of a PrtgNode tree, rewriting parent nodes when child nodes are changed
PrtgNodeDefaultVisitor void PrtgNodeVisitor Visits a single PrtgNode and performs a common action for each node by default
PrtgNodeDefaultVisitor<T> T PrtgNodeVisitor<T> Visits a single PrtgNode and performs a common action for each node that returns a value of type T by default

The following table outlines the scenarios each visitor should be used, and provides examples of where the visitor has been used within PrtgAPI for reference purposes.

I want to... Recommended Visitor Examples
Read all the nodes in a tree, performing a common action for most or all node types PrtgNodeDefaultVisitor PrtgNodePrettyTreeVisitor
Read all the nodes in a tree, performing a different action for each node type PrtgNodeWalker
Modify the nodes of a tree PrtgNodeRewriter PrtgNodeListEditor, PrtgNodeRemover, PrtgNodeReplacer
Transform the nodes of a tree into completely unrelated types, performing a common transformation for each node type PrtgNodeDefaultVisitor<T> CompareTreeVisitor
Read a custom set of nodes from a tree, potentially performing a different transformation on each node type PrtgNodeVisitor, PrtgNodeVisitor<T>

Generally speaking, PrtgNodeDefaultVisitor, PrtgNodeWalker and PrtgNodeRewriter are sufficient for most purposes. Even when the purpose of your visitor is to construct some value that should eventually be returned, it is often best to do so using an instance member, rather than returning this value over and over again as you would in visitors like PrtgNodeVisitor<T>.

C#

PowerShell