Hi,
I would like to build a hierarchy of ProductNode objects like so
ProductNode
---Product Node
---ProductNode
------ProductNode
------ProductNode
---ProductNode
---ProductNode
------ProductNode
etc
The data that populates these objects comes from a stored procedure that
returns it in the following format:
Depth Id ParentId
0 1 0
1 2 1
1 3 1
2 45 3
2 56 3
3 98 56
2 89 3
etc
The definition of the ProductNode class is as follows (unfortunately in VB):
Public Class ProductNode
Private _key As String = String.Empty
Private _product As Product
Private _children As ArrayList = New ArrayList
' Expose the product depth property
Public ReadOnly Property Depth() As Int16
Get
Return _product.Depth
End Get
End Property
' Expose the product ID property
Public ReadOnly Property ID() As Int32
Get
Return _product.ID
End Get
End Property
' Expose the product ParentID property
Public ReadOnly Property ParentID() As Int32
Get
Return _product.ParentID
End Get
End Property
'Expose the product Name property
Public ReadOnly Property Name() As String
Get
Return _product.Name
End Get
End Property
'Constructor that stores the product and creates a key based upon the
product ID and depth
Public Sub New(ByVal product As Product)
_key = Convert.ToString(product.ID) + "," +
Convert.ToString(product.Depth)
_product = product
End Sub
'Adds a product to the children of this product node
Public Sub Add(ByVal productNode As ProductNode)
_children.Add(productNode)
End Sub
'Removes a product from the children of this product node
Public Sub Remove(ByVal productNode As ProductNode)
_children.Remove(productNode)
End Sub
End Class
I am then using the following method to populate the entire hierarchy. The
first time this is called from another method the productnode that is passed
as a parameter is the root node:
Private Sub BuildHierarchy(ByVal parentNode As ProductNode)
' Read the next item in the data
_productData.Read()
' Get all the product fields
Dim name As String = Convert.ToString(_productData("Name"))
Dim id As Int32 = Convert.ToInt32(_productData("Idx"))
Dim depth As Int16 = Convert.ToInt16(_productData("Depth"))
Dim parentID As Int32 = Convert.ToInt32(_productData("ParentIdx"))
' If this item is a child of the passed in parent add it
If parentID = parentNode.ID And depth = parentNode.Depth + 1 Then
'Instantiate a new product with these fields
Dim thisProduct = New Product(name, id, depth, parentID)
'Instantiate a new product node for the tree that contains the
product
Dim thisProductNode As New ProductNode(thisProduct)
'Add this node to the parent
parentNode.Add(thisProductNode)
' Recurse this function using the current product node
BuildHierarchy(thisProductNode)
End If
End Sub
This works fine for the first branch of the hierarchy and will product an
output similar to:
All Products
---Product branch 1 level 1
------Product branch 1 level 2
---------Product branch 1 level 3
------------Product branch 1 level 4
------Product branch 2 level 2 <-- next item not sure how to get
However, I am unsure of how to then populate the rest of the hierarchy. The
rows returned from the stored procedure are returned in such a form that
branch is populated from left to right. So, for example in the above example
the rows would come out in the following order:
All Products
Product branch 1 level 1
Product branch 1 level 2
Product branch 1 level 3
Product branch 1 level 4
Product branch 2 level 2 <-- next item not sure how to get
So, the BuildHierarchy function above would recurse through until "Product
branch 1 level 4" and then exit. The next row in the returnset from the
stored procedure is "Product branch 2 level 2". I cannot think of how to add
this child to its parent (Product branch 1 level 1) within the object model.
Perhaps I could use the key property of the object to navigate to the parent
directly, but I would have to search through the entire hierarchy to do this
and it would take time considering there could be thousands of objects.
I was also contemplating the possibility of somehow linking the parent from
the child, but was not sure whether this would be good practice, relevant or
possible.
Does anyone have any ideas on how I could achieve this?
Many thanks in advance. If you have read the whole thing I am impressed!
A