title: Eager Loading aggregate roots with Entity Framework
body: <p>I would like to create a more structured approach to loading the needed entity-tree:</p>
  
  <p>I need a serious amount of data, so I'm doing this using type-safe Includes (just a normal Include but with Lambda's) <a href="http://www.codetuning.net/blog/post/Entity-Framework-compile-safe-Includes.aspx" rel="nofollow noreferrer">as shown here</a>.</p>
  
  <p>As I said, I need a lot of data, basically a whole entity tree under 1 parent item.</p>
  
  <p>Now, I could do this doing something like:</p>
  
  <pre><code>context.House
      .Include(x =&gt; x.Doors)
      .Include(x =&gt; x.Doors.FirstOrDefault().Joint)
      .Include(x =&gt; x.Doors.FirstOrDefault().Joint.FirstOrDefault().JointCategory)
      .Include(x =&gt; x.Doors.FirstOrDefault().Joint.FirstOrDefault().JointCategory.JointType)
      .Include(x =&gt; x.Windows)
      // ... same thing
      .Include(x =&gt; x.Roof)
      // ... same thing
  </code></pre>
  
  <p>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)</p>
  
  <p>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.</p>
  
  <p>This probably does not make much sense so:</p>
  
  <pre><code>public void LoadHouse(int id)
  {
      // ...
      ObjectQuery&lt;House&gt; query = context.House;
  
      // and now?
      LoadDoors(query, x =&gt; x.Door);
  
  }
  
  public void LoadDoors&lt;T&gt;(ObjectQuery&lt;T&gt; query, ..?..)
  {
      // ... ?
  
      LoadJoints(...)
  
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Has anyone done something like this? Or could anyone give me some pointers?</p>