- Often times, to represent hirerachy structure, a custom class is created to hold original data.
- We will have recursive functions to select nodes at various levels and build tree structure.
Lets take a example of Employee. Employees are managed by Manager, who themselves are employees and also managed by some other employee.
Code:
class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int ManagerID { get; set; }
}
And we have following class to represent Hirerachy structure
Code:
public interface INode
{
List
Object PayLoad { get; set; }
bool IsSelected { get; set; }
}
public class Node:INode
{
public List
public Object PayLoad { get; set; }
public Node()
{
ChildNodes = new List
}
}
Before going further lets think of nature of conditions which we will be applying to Employee class to arrange it in hierarchy structure:
Employees who doesnt have any managerId are super employees like CEO who wont report to any other employee.
Condition for root employees: employee.ManagerID == default(int)
Employee child nodes will be those employees whose manager id is current employee's Id.
Condition which needs to be applied recursively is : parent.ID == child.ManagerID && child.ManagerID != default(int)
Using HirerachyData Structure you can compose your nodes as below:
List
.RootNodesFilter((p) => p.ManagerID == default(int)) //How root nodes to be selected.
.ChildNodeFilter((parent, child) => parent.ID == child.ManagerID && child.ManagerID != default(int)) //What condition to check recursively to build hirerachy
.UsingObjectBuilder((d) => new Node() { PayLoad = d }) //And finally how to build node.
.BuildHeirarchy();
Using this approach , you can compose how to select root, how to select nodes recursively, and even you can specify how to build final nodes (could be any type), due to which you can get the power to build any type of hirerachy structures with any properties (NOTE: code listing below is coupled to INode, but you can safely avoid that as Object builder will take care of instancing out of hirerachydatasource).
HirerachyDataSource code listing is below:
Code:
public class HirerachyDataSource
{
public static CompositionStructure ComposeHirerachyUsingStructure(List
{
CompositionStructure structure = new CompositionStructure(NodesList);
return structure;
}
public class CompositionStructure
{
private List
Predicate
Func
Func
public CompositionStructure UsingObjectBuilder(Func
{
ObjectBuilder = nodeBuilder;
return this;
}
public ObservableCollection
{
return BuildNode();
}
internal CompositionStructure()
{
}
internal CompositionStructure(List
{
this.FlatStructure = nodes;
}
public CompositionStructure RootNodesFilter(Predicate
{
rootNodeSelector = rootNodesFilter;
return this;
}
public CompositionStructure ChildNodeFilter(Func
{
childNodeSelector = childNodeFilter;
return this;
}
protected ObservableCollection
{
//INode node = new Node();
List
where rootNodeSelector(a)
select ObjectBuilder(a)).ToList();
if (nodes != null)
{
foreach (var item in nodes)
{
BuildNode(item);
}
}
return new ObservableCollection
}
protected void BuildNode(INode node)
{
if (node != null)
{
List
where childNodeSelector((T)node.PayLoad, a)
select ObjectBuilder(a)).ToList();
node.ChildNodes = new ObservableCollection
if (node.ChildNodes != null)
{
foreach (var item in node.ChildNodes)
{
BuildNode(item);
}
}
}
}
}
}