tags:

views:

53

answers:

4

Hi,

Does anyone know of a solid C# library / approach to manage a hierarchy/web type collection?

This would be a library that would basic consist of the concept of nodes & relationships, for example to model web pages/files linked under a URL, or modeling IT infrastructure. It would have key methods such as:

  • Node.GetDirectParents()

  • Node.GetRootParents()

  • Node.GetDirectChildren()

  • Node.GetAllChildren()

So it's smarts would include the ability to "walk the tree" of nodes based on the relationships when someone does ask for "give me all the children under this node" for example.

It ideally include a persistence layer, to save/retrieve such data to/from a databases (e.g. with a Nodes and Relationships table).

EDIT 1

  • Also note that it has to support the full open flexibility that there are nodes & relationships, and hence a node may be a child of more than one node. That is it can model more of a web map as opposed to a strict hierarchy.
  • Also then a Node may have more than one root/parent node (e.g. in the use case of modeling web page artefacts an image may be referenced on more than one web-site)
A: 

SiteMapNode class?

http://msdn.microsoft.com/en-us/library/system.web.sitemapnode_properties(v=VS.100).aspx

Raj Kaimal
Raj - sounds closer than TreeNode, however do you know if it supports modeling a web map of objects as opposed to a pure hierarchy? For example one node should be able to have more than one root/parent node? One node could be a child of multiple other nodes, and have multiple children itself?
Greg
A: 

The approach is easy, simply include a reference to an instance of self called 'Parent' and a collection of 'Children'. You can enforce this using the class constructor: require the Parent to be passed to the constructor and simply set Parent to that value and then add 'this' to the Children collection on the parent class.

e.g.

   public class Whatever
   {
      public Whatever Parent {get; private set;}
      protected List<Whatever> Children {get; private set;}


      public Whatever(Whatever parent)
      {
          this.parent = parent;
          this.Children = new List<Whatever>();
          if (parent != null)
          {
              parent.Children.Add(this);
          }
      }

      public IEnumerable<Whatever> AllChildren
      {
         return this.Children.Union(this.Children.SelectMany(child => child.AllChildren));
      }

With this in place AllChildren, DirectChildren, Root are all easily implemented.

When you serialize to a database you only need to record the ParentID for each instance and can rebuild the tree after loading them all. The node with no parent is the root.

Hightechrider
Greg
Replace "TreeNode" with whatever class you are creating. Just add a constructor like this, a property 'Parent' and a List<whatever> Children.
Hightechrider
I'm looking for a library that has the smart of "walking the tree" of nodes/relationships when asked to do a "GetChildren" for example. Does this make sense. Where would the existing code for this searching be exactly in what you're suggesting? Or are you suggesting I would then have to write this (which defeats the purpose of the goal to find a library that implements this)
Greg
Example added, it's not hard.
Hightechrider
Or did you mean 'directed graph' rather than 'hierarchy'?
Hightechrider
umm, I'm not sure re what 'directed graph' means exactly, however in the example it doesn't cater for the fact a node can have multiple parents. You could add this in but then I was looking for a nice library with API's to manage this, including a means to persist (e.g. to database).
Greg
PS. In fact given the graph has to be persisted to database then in the first instance perhaps the library would in fact work directly to database as opposed to creating a full in-memory graph, as this would then raise design questions around synchronization of the in-memory graph with the database. So the GetAllChildren call for example may have the library going to the database and walking the Nodes (via relationships) to gather together and return the final List<Node> to the client. Make sense?
Greg
+1  A: 

There are various samples on the web for how to create a directed graph (which is what you are actually looking for). For example http://www.jrcalzada.com/post/2010/02/14/Generic-Graph-Class-in-C.aspx

Hightechrider
Greg
PS. Just came across http://quickgraph.codeplex.com/ so having a look at that now too...
Greg
RE: Persistence: Start with Entity Framework (preferably v4) - you can either create the database first and then generate all the classes from it or create the classes first and then add persistence.
Hightechrider
+1  A: 

QuickGraph is the closest thing I've found so far...

http://quickgraph.codeplex.com/

Greg
+1 for QuickGraph. It's very thorough and well-designed.
Doug