public class Node
{
// General properties
public List<Relationship> Relationships { get; set; }
}
public class Relationship
{
public Node Parent { get; set; }
public Node Child { get; set; }
public RelationshipType Type { get; set; }
}
public enum RelationshipType
{
//...
}
The most important (and easily botched) component of this is the Relationships property on the Node class. The way I defined is the easiest way, but a more reliable way would be to model it in more of a database fashion, where you have a central relationship repository and the Node is connected.
public class RelationshipRepository
{
private List<Relationship> relationships = new List<Relationship>();
public IEnumerable<Relationship> GetRelationships(Node node)
{
return relationships.Where(r => r.Child == node || r.Parent == node);
}
public IEnumerable<Relationship> GetChildRelationships(Node node)
{
return relationships.Where(r => r.Parent == node);
}
public IEnumerable<Relationship> GetParentRelationships(Node node)
{
return relationships.Where(r => r.Child == node);
}
public void Add(Node parent, Node child, RelationshipType type)
{
relationships.Add(new Relationship()
{
Parent = parent,
Child = child,
Type = type
});
parent.RelationshipSource = this;
child.RelationshipSource = this;
}
}
public class Node
{
// General properties
public RelationshipRepository RelationshipSource { get; set; }
public IEnumerable<Relationship> Relationships
{
get { return relationships.GetRelationships(this); }
}
public IEnumerable<Relationship> Children
{
get { return relationships.GetChildRelationships(this); }
}
public IEnumerable<Relationship> Parents
{
get { return relationships.GetParentRelationships(this); }
}
}
This would allow for you to create a single RelationshipRepository instance, add your relationships between Nodes using the Add function, and it will take care of the rest. Subsequent calls to Relationships, Children, or Parents on one of the affected Nodes will automatically examine the relationshipSource to determine the children, parents, or all relationships.