views:

566

answers:

4

I have an application that reads a table from a database.

I issue an SQL query to get a result set, based on a unique string value I glean from the results, I use a case/switch statement to generate certain objects (they inherit TreeNode BTW). These created objects get shunted into a Dictionary object to be used later.

Whilst generating these objects I use some of the values from the result set to populate values in the object via the setters.

I query the Dictionary to return a particular object type and use it to populate a treeview. However it is not possible to populate 2 objects of the same type in a treeview from the Dictionary object (you get a runtime error - which escapes me at the moment, something to with referencing the same object). So what I have to do is use a memberwiseClone and implement IClonable to get around this.

Am I doing this right? Is there a better way - because I think this is causing my program to be real slow at this point. At the very least I think its a bit clunky - any advice from people who know more than me - greatly appreciated.

+1  A: 

Is there a reason you are using the external dictionary? I would populate the tree directly as the data is queried.

If you do require the dictionary, you could set the .Tag property of the tree node to point to the data in your dictionary.

Brad Bruce
A: 

To add to @Brad, only populate the tree as needed. That means hooking into the expand event of the tree nodes. This is similar to how Windows Explorer functions when dealing with network shares.

There should be 1 TreeNode object per actual tree node in the tree - don't try to reuse the things. You may either associate them with your data using the Tag property (this is the recommended method), or you can subclass the TreeNode itself (this is the Java method, but used less in .NET).

(The use of cloning methods is usually a hint that you're either (a) doing something wrong, or (b) need to factor your domain model to separate mutable objects from immutable.)

Frank Krueger
A: 

Well the main reason I'm using a Dictionary is that the user will create a treenode themselves at some random time - and it is hard to go back to the database to try and query for the correct information to generate the node-object from scratch. It seems much easier to query once and store in a Dictionary rather than keep going back to the database - well that was my initial reasoning :)

Note: I have subclassed the TreeNode to create my own customised node that does lots of handy stuff!

Vidar
A: 

have you considered using a Virtual Tree view which only loads the nodes the user actually wants to look at - i've had good success with the component from www.infralution.com

GreyCloud