How can I make this code more generic
- by Greg
Hi
How could I make this code more generic in the sense that the Dictionary key could be a different type, depending on what the user of the library wanted to implement? For example someone might what to use the extension methods/interfaces in a case where there "unique key" so to speak for Node is actually an "int" not a "string" for example.
public interface ITopology
{
Dictionary<string, INode> Nodes { get; set; }
}
public static class TopologyExtns
{
public static void AddNode(this ITopology topIf, INode node)
{
topIf.Nodes.Add(node.Name, node);
}
public static INode FindNode(this ITopology topIf, string searchStr)
{
return topIf.Nodes[searchStr];
}
}
public class TopologyImp : ITopology
{
public Dictionary<string, INode> Nodes { get; set; }
public TopologyImp()
{
Nodes = new Dictionary<string, INode>();
}
}