views:

315

answers:

1

I would like to create a more structured approach to loading the needed entity-tree:

I need a serious amount of data, so I'm doing this using type-safe Includes (just a normal Include but with Lambda's) as shown here.

As I said, I need a lot of data, basically a whole entity tree under 1 parent item.

Now, I could do this doing something like:

context.House
    .Include(x => x.Doors)
    .Include(x => x.Doors.FirstOrDefault().Joint)
    .Include(x => x.Doors.FirstOrDefault().Joint.FirstOrDefault().JointCategory)
    .Include(x => x.Doors.FirstOrDefault().Joint.FirstOrDefault().JointCategory.JointType)
    .Include(x => x.Windows)
    // ... same thing
    .Include(x => x.Roof)
    // ... same thing

As you can see, this line filled with includes can get quite huge. This is in fact a very simplified sample of the actual code (which doesn't include houses btw)

So what I would like to do is creating methods, responsible for its branch in the tree. Where the method can accept the object query and include the child, and in its turn, call the "child-loader methods". Also, the parent shouldn't matter, as long as it has a property with the type of the child.

This probably does not make much sense so:

public void LoadHouse(int id)
{
    // ...
    ObjectQuery<House> query = context.House;

    // and now?
    LoadDoors(query, x => x.Door);

}

public void LoadDoors<T>(ObjectQuery<T> query, ..?..)
{
    // ... ?

    LoadJoints(...)


}

And so on. But I can't really get my head around it... There's a missing link between the incoming query and calling the child methods.

Has anyone done something like this? Or could anyone give me some pointers?

+2  A: 

Try something like this instead:

query = LoadDoors(query, x => x.Door);

Where LoadX returns the result of calling Include.

Andrew Peters
I don't understand the need to pass in the expression to the LoadDoors method, surely the LoadDoors method knows that expression?
Alex James
I also don't see how changing void to return query is a solution to this. Afterall, the query comes in byref, so whatever I do to it in the method will be done.
Bertvan
@Bertvan because ReferenceEquals(q, q.Include("Foo")) == false
Andrew Peters
@andrew: so true, I've just struggled with this all day yesterday. Thanks, this is a correct answer...
Bertvan