I have a Tree.
class TreeNode {
public TreeNode(string name, string description) {
Name = name;
Description = description;
}
string Name { get; set; }
string Description { get; set; }
public List<TreeNode> Children = new List<TreeNode>();
}
I would like to populate a large one for unit testing purposes. I would really like to keep stuff DRY.
Say for illustration purposes my tree has the following structure
Parent,desc Child 1, desc1 Grandchild 1, desc1 Child 2, desc2
How would you go about populating the tree in an elegant an maintainable way?
I find this code quite repetitious and error prone:
var parent = new TreeNode("Parent", "desc");
var child1 = new TreeNode("Child 1", "desc1");
var child2 = new TreeNode("Child 2", "desc2");
var grandchild1 = new TreeNode("Grandchild 1", "desc1");
parent.Children.Add(child1);
parent.Children.Add(child2);
child1.Children.Add(grandchild1);
EDIT
I ended up doing the DSL approach:
A demo test lives here.
It uses a builder and a simple DSL.