() . , AddRelationship ? ? , - , node ?
, - :
public interface INode<T>
{
INode<T> Parent { get; }
IEnumerable<INode<T>> Children { get; }
String Key { get; }
void AddChild(INode<T> child);
}
public class Node<T> : INode<T>
{
public Node(String key) : this(key, null) {}
public Node(String key, INode<T> parent)
{
this.Parent = parent;
this.Children = new List<T>();
this.Key = key;
}
public virtual INode<T> Parent { get; protected set; }
public virtual String Key { get; protected set; }
public virtual List<T> Children { get; protected set; }
public void AddChild(INode<T> node)
{
this.Children.Add(node);
}
}
IRelationship. INode .
( OP):
, API , , :
public static bool AddRelationship(this INode<T> node, IRelationship<T> relationship)
{
if (node.Relationships == null)
node.Relationships = new List<T>;
if (relationship == null) throw new ArgumentNullException("relationship");
node.Relationships.Add(relationship);
return true;
}
:
INode node = new Node<String>("some key");
INode someParent = new Node<String>("some parent key");
INode someChild = new Node<String>("some child key");
node.AddRelationship(new RelationshipImp(someParent, someChild));
, . , , .
, "node, ", "node "? , . - , ? , , .
, , :
public static bool AddRelationship<T,R>(this ITopology<T> top, INode<T> parent, INode<T> child ) where R : IRelationship, new
{
IRelationship<T> rel = new R();
rel.Parent = parent;
rel.Child = child;
top.Relationships.Add(rel);
}
:
ITopology<String> top = new TopologyImp<String>;
top.AddRelationship<RelationshipImp>(new NodeImp("parent"), new NodeImp("Child"));