BO design to impement n-ary and n-level tree

BO design to impement n-ary and n-level tree

Old forum URL: forums.lhotka.net/forums/t/3083.aspx


sai posted on Monday, June 25, 2007

Hello,

I need help preparing design to implement n-ary and n-level tree retaining all the benefits provided by CSLA framework. All nodes are of the same type say NodeType.

One approach, I think, is create type NodeType : BusinessBase<NodeType> and NodeTypes : BusinessListBase<NodeTypes,NodeType>. NodeType will hold reference to NodeTypes corresponding to it's children and so on.

Another approach is create type NodeType : BusinessBase<NodeType> and NodeTypes : BusinessListBase<NodeTypes,NodeType>. NodeType will hold array of ids corresponding to it's children and all queries to retrieve hierarchical information will answered by NodeTypes object. With this approach each hierarchy will have single NodeTypes object.

Also I don't want to use lazy loading as I am required to retrieve information about entire hierachy in single request.

And most important is that I want to retain all the benefits of CSLA framework. Which of the above approach will satisfy this or is there any better one.

Thanks

 

 

 

 

hurcane replied on Monday, June 25, 2007

Our project involves n-level assemblies for fabrication in a shop. We chose to have each node in our assembly contain a variable of the nodes collection. This gives us the structure that is needed in most use cases.

Some of our use cases work better with a flat list of the nodes. We have a shared method on the Node class that takes a node and returns a flat list of the nodes.

josh.kodroff replied on Tuesday, June 26, 2007

Sai,

Interestingly enough, I just had to do this myself.  While I've only gotten fetching done (correctly), here's what I've come up with:

First off, I'm using a (simplified) table structure like this:
Tree: tree_id, tree_name
Tree_Node: tree_node_id, tree_node_name, tree_node_parent_id (null when the node is the root)

With this table structure, you can retrienve all nodes of a tree in a single query.  Round trips to the DB server are very expensive.

and an object structure like this:

Class Tree
    mName as string
    mRoot as TreeNode

Class TreeNode
    mParent as TreeNode <- Very important!  Make sure you mark this one as <NonSerialiable> <NotUndoable> or you'll get an infinite loop when the framework tries to copy state.  You'll also need to write code for the Deserialization event.
    mChildren as TreeNodeList

I'm using the CSLA templates as the basis for my classes.

When I'm fetching, the Tree class executes an sproc that does this:
select * from tree where tree_id = @x
select * from tree_node where tree_id = @x

Then I pass a datatable (not a datareader) to the root.  The fetch for the children should have a signature of (dt as DataTable, parent as TreeNode).

The root knows it's parent is nothing, so it looks for the row where tree_node_parent_id is null.

Then, for each row where tree_node_parent_id = Me.mParentId, I create a new TreeNode, but you need to be careful to mark the row as deleted as you cycle through the list, or you can get stuck in an infinite loop, reading the same children over and over since this is an n-ary tree.

For writing to the db, I plan to use a function that returns the nodes as a datatable, copying the IsNew and IsDirty fields so that the Tree class can write the nodes to the db.

Does that help?

sai replied on Friday, June 29, 2007

Josh, Thanks.

I have started working on my second approach and although this means that I have flattened the tree, hierarchy get build the way you are doing. Your explanation helped lot.

 

Copyright (c) Marimer LLC