Print Page | Close Window

Retrieving Data Based On A Hierachical Model

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=334
Printed Date: 14-Apr-2026 at 4:24pm


Topic: Retrieving Data Based On A Hierachical Model
Posted By: jozza
Subject: Retrieving Data Based On A Hierachical Model
Date Posted: 26-Jul-2007 at 11:47pm
Hi,
 
Say for example I have a hierarchical table as follows:
 
Id          Level              Name             Parent
1           1                    Bob                 NULL
2           1                    Steve              NULL
3           2                    Rachel             1
4           2                    Brian               1
5           2                    Colin               1
6           2                    Tracy               2
7           2                    Sally                2
8           3                    Trevor             6
9           3                    Reginald          6
10         3                    Sue                 6
11         4                    Boris               8
 
So we have a hierarchy which is n levels deep. Where for example, Tracy's boss is Steve and Tracy has Trevor, Reginald, Sue underneath her as direct reports and Boris as an indirect report.
 
No I'd like to create a query that given an Id, will return all the reports for the given person as well as being able to reference their bosses, but at the sametime not allowing to traverse back down the tree from the boss level. Let me give an example to make this clear.
 
Say we pass in 6 (Tracy) to the query i want to get back Trevor, Reginald and Sue, so basically Tracy.Reports.Count = 3. I should also be able to go Tracy.Reports[Trevor].Reports.Count = 1. Excuse the invalid syntax, but it gives you an idea of what i'm getting at. ie. being able to traverse down the tree. Also, I'd like to be able to go Tracy.Boss which returns Steve. Now that i have the Steve entity object i want to be able to only traverse back down the Tracy path. ie. I shouldn't be allowed to go Steve.Reports[Sally], because Sally wasn't in the result of the original query. I hope that makes sense.
 
Thanks.



Replies:
Posted By: IdeaBlade
Date Posted: 27-Jul-2007 at 11:17am
This is a good example of where you want to use "navigation", not a "query".  Developers new to DevForce often think they should obtain business objects using a query whereas in practice navigation often works much better.
 
For my example, I use the IdeaBladeTutorial Datababase, and assume that all individuals belong to the Employee table.  The name of the relationship for "Boss" is "Manager.  The name of the relationship for "direct reports" is "DirectReports".  See IdeaBlade Tutorial on "Populating a WinForm" for concrete examples.
 
 
Example -  Print out all of Tracy's direct reports and her manager  by:
 

PrimaryKey key = new PrimaryKey(typeof(Employee), 6);

Employee Tracy = (Employee)mPM.GetEntity(key);

foreach (Employee emp in Tracy.DirectReports) {

MessageBox.Show(emp.FullName);

}
 
MessageBox.Show(Tracy.Manager.FullName);
 
 
 
 


Posted By: jozza
Date Posted: 29-Jul-2007 at 4:30pm
Well.... that kinda doesn't really answer my question. I'm aware that you can use navigation to view "direct reports" but my question is about how you can restrict navigation. ie. Only allow the user to navigate down certain direct reports based on say, some security setting.
 
So in your example the foreach statement only has a subset of Tracy's direct reports in Tracy.DirectReports.
 
Thanks.


Posted By: davidklitzke
Date Posted: 31-Jul-2007 at 8:36am

There are a couple of different approaches you could use.  You could either define a new property or override an existing property, or you could build an event handler to dynamically handle your problem, or both.

(1) Create a new property
 
You could create a new property (e.g., "RestrictedEmployees" or "RestrictedEmployeeGraph") that would provide a ReadOnly EntityList or an EntityGraph of those employees that the current user had access to.
 
(2) Override an existing property
 
For example, the property Employee.DirectReports currently provides a ReadOnly list of all direct reports .  You could override this property to provide the subset of Employees that should be visible to the user.  You could also use an EntityGraph to capture a hierarchy of Employees.  One advantage of an override is that this approach might be implemented with the least amount of code.
 
(3) Use an event handler
 
You could use an event (e.g., button click, or double-click on a row in the grid) and then provide an event handler to dynamically process the event.  For example, it could discover the currently selected Employee, and then compute the list of Employees that could be seen.
 
 



Print Page | Close Window