Recursive Prefetch Paths

Posts   
 
   
 
Anonymous
User
Posts: 0
Joined: 11-Nov-2006
# Posted on: 26-Jun-2007 16:54:42   

Hi Guys,

Content management systems often have hierarchial relationship Pages / Categories etc. Its not complicated to do in code but as a covenience could you implement recursive prefetch paths?

Cheers,

Pete

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39753
Joined: 17-Aug-2003
# Posted on: 26-Jun-2007 18:42:36   

You can define the prefetch today if you want (though it will perform poorly for obvious reasons), but I guess what you're really saying is that you want a single fetch on the table and a tree is build from that data, correct?

Frans Bouma | Lead developer LLBLGen Pro
Anonymous
User
Posts: 0
Joined: 11-Nov-2006
# Posted on: 26-Jun-2007 20:07:16   

Yip. I've always found hierarchial data to be a pain and I suspect I'm not alone. I know a few people have tried to implement a nested-set using the code generated by llblgen but I don't know if anyone actually succeeded. That's not a criticism of llblgen, I love using it but I would love it more if abstracted away tree management. hehe .. and mult-language support ( i thought i'd just throw that one in case you have some spare time! simple_smile )

Cheers,

Pete

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39753
Joined: 17-Aug-2003
# Posted on: 27-Jun-2007 10:38:21   

lad4bear wrote:

Yip. I've always found hierarchial data to be a pain and I suspect I'm not alone. I know a few people have tried to implement a nested-set using the code generated by llblgen but I don't know if anyone actually succeeded. That's not a criticism of llblgen, I love using it but I would love it more if abstracted away tree management. hehe .. and mult-language support ( i thought i'd just throw that one in case you have some spare time! simple_smile )

heh spare time... wink

I'll show you a little trick, which will give you an O(n) routine to build up a tree from a single set of entities. You might already know this, but in case you don't, it might help simple_smile

Say, you have an Employee entity and it has a reference to itself called 'Manager' and has an FK field called 'ManagerID', which can be null, which means it's the root.

To build the tree from root to leafs, we just have to walk the list twice. One run over the list is for speeding up the retrieval of parent nodes. For small sets this isn't really necessary.


EntityCollection<EmployeeEntity> employees = new EntityCollection<EmployeeEntity>();
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.FetchEntityCollection(employees, null);
}

Dictionary<int, EmployeeEntity> idToEmployee = new Dictionary<int, EmployeeEntity>();
foreach(EmployeeEntity e in employees)
{
    idToEmployee.Add(e.Id, e);
}

EmployeeEntity root = null;
// build the hierarchy
foreach(EmployeeEntity e in employees)
{
    if(e.TestCurrentFieldValueForNull(EmployeeFieldIndex.ManagerId))
    {
        // root
        root = e;
        continue;
    }
    EmployeeEntity manager = null;
    if(!idToEmployees.TryGetValue(e.ManagerId, out manager))
    {
        // found an employee with a manager which doesn't exist.
        throw new Exception("Please implement FK constraints on the Employee table!");
    }
    e.Manager = manager;
}

That's it. simple_smile

You can make this generic by passing in a relation, and other elements to make the function generic instead of dependant on the structure of the entity.

Frans Bouma | Lead developer LLBLGen Pro