Hierachial Object

Hierachial Object

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


alfredg posted on Monday, August 20, 2007

I am working with a table with a parent child relationship to itself. For example

Employee[employeeid, name, managerid] where manager is is a relation back to employeeid.

I created a readonlylist class called EmployeeList and a readonlyobject class called EmployeeInfo. The challenge I have run into is how to populate a ManagedEmployees property of the EmployeeInfo class.

Here is the constructor for the EmployeeInfo class. I assume this is the place I need to work as this is the point in which the parent object's values are populated.

internal EmployeeInfo(SafeDataReader dr){
_EmployeeID = dr.GetInt32(
"EmployeeID");
_EmployeeName = dr.GetString(
"EmployeeName");
_ManagerID = dr.GetInt32(
"ManagerID");

// LoadChildren
_ManagedEmployees = ????
}

Aefried

skagen00 replied on Monday, August 20, 2007

Thinking about what you want for your result set, you want a list of individuals each potentially containing a list of individuals - in many respects you have a grandchild situation.

A question might be - how many employees do you think you'll be dealing with? This might have an impact on what you do for retrieval, along with your use cases for what you want to do with this object.

That is, if you had 500 or so, you might want to have _managedEmployees be entirely lazy loaded through a command object - that is, you retrieve your employeeId, employeeName, and managerId as you have above, but when it comes to managed employees what you may do is this:

public EmployeeList ManagedEmployees

{

   if (_managedEmployees == null)

   {

      _managedEmployees = EmployeeList.GetManagedEmployees(_employeeId);

   }

   return _managedEmployees;

}

This GetManagedEmployees would perhaps use the same DataPortal.Fetch as EmployeeList.GetUnmanagedEmployees (GetTopLevelEmployees) (perhaps the top level retrieve where maybe you only get the president/CEO - people with no managers) - just would have an employee Id in the criteria.

Thus you could easily set up a treeview concept where it'll retrieve exactly how much you demand of it.

Really it depends on your use cases - what are you trying to enable?

Chris

triplea replied on Tuesday, August 21, 2007

Providing you construct your query correctly you can then use the deep data example of Rocky's to load children, grandchildren etc. It can be found here: http://www.lhotka.net/Article.aspx?area=4&id=7fbb619a-ba81-4157-b434-8756b68ccbd0

I guess the main idea is that you select all your managers (select * from Employee where managerid is null ?) and in the same select/stored proc, all the non-manager employees (select * from Employee where managerid is not null). Then you can populate your children with the first result set and grand children with the next result set (as per the above link).

Copyright (c) Marimer LLC