views:

262

answers:

1

I currently have an extension Method which converts an IEnumerable of type Tab into a hierarchical collection of TabNodes.

// If Tab has no parent its ParentId is -1

public class Tab
{
public int TabId { get; set; }
    public string TabName { get; set; }
    public int Level { get; set; }
    public int ParentId { get; set; }

}

public class TabNode
{
    public TabInfo Tab { get; set; }
    public IEnumerable<TabNode> ChildNodes { get; set; }
    public int Depth { get; set; }
}

For instance, the following would give you a collection of TabNodes who are below a Parent with TabId 32 - the maximum level of depth is 4.

IEnumerable<Tab> tabs = GetTabs();

IEnumerable<TabNode> = tabs.AsNavigationHierarchy(32,4);

This is confusing and not very friendly for further refinement. What If I'd like to specify a certain Level instead of a ParentID?

What I'd like to do is something like this:

IEnumerable<TabNode> = tabs.AsNavigationHierarchy().WithStartLevel(2).WithMaxDepth(5)

I'm stuck as how to do this elegantly. Can you help me?

This is my current function which is called by my extension methods (based on an article I've found on www.scip.be).

    private static IEnumerable<TabNode>
      CreateHierarchy(
        IEnumerable<TabInfo> tabs,
        int startTabId,
        int maxDepth,
        int depth)
    {
        IEnumerable<TabInfo> children;


            children = tabs.Where(i => i.ParentId.Equals(startTabId));


        if (children.Count() > 0)
        {
            depth++;

            if ((depth <= maxDepth) || (maxDepth == 0))
            {
                foreach (var childTab in children)
                    yield return
                      new TabNode()
                      {
                          Tab = childTab,
                          ChildNodes =
                            CreateHierarchy(tabs, childTab.TabID, maxDepth, depth),
                          Depth = depth
                      };
            }
        }
    }
+2  A: 

tabs.AsNavigationHeirachy could return a HerirchyQuery object which your next extension methods would then expect. This will let you chain them together.

JoshBerke
+1 But at that point, the rest of the methods would probably actually be members of the HierarchyQuery instead of Extension Methods.
Chris Shaffer
That's what I thought about initially.. unfortunately my teammates would expect it to return the an IEnumerable<TabNode> with the entire navigation structure if only AsNavigationHierarchy() was called. But actually that's a good idea indeed...
kitsune
Yes Chris, I guess it would be some kind of normal builder then
kitsune
Concession for your teammates might be that you always have to end with .Execute() [or some other method] that actually executes the query and returns the IEnumerable<TabNode>.
Chris Shaffer
Return an object which Implements IEnumerable<TabNode> but also has the limits to limit the output.
JoshBerke
I'm actually Implementing IEnumerable<Tab> now... thanks for the input
kitsune
Welcome good luck
JoshBerke