title: Includes in a (semi) generic Repository
body: <p>I am trying to figure out how to use the new EF Code First stuff and I am having trouble figuring out how to adapt the <code>Include</code> functionality into a semi-generic Repository class. (I say semi-generic, because the class is not generic, just the methods. So I have one repository that wraps an aggregate, essentially, and you can interact with all Entities that are part of that aggregate, not just a single Entity.)</p>
  
  <p>My situation is I have an Entity that has one child that <em>always</em> needs to be loaded, and then other children that are only optionally loaded, I want to include a simple bool parameter on my repository method. Like so:</p>
  
  <pre><code>public S GetByID&lt;S&gt;(int entityID, bool loadChildren = false) where S : class
  {
    DbSet&lt;S&gt; set = _context.Set&lt;S&gt;();
  
    if (loadChildren)
      set = FullInclude&lt;S&gt;(set);
    else
      set = DefaultInclude&lt;S&gt;(set);
  
    return set.Find(entityID);
  }
  
  protected virtual DbSet&lt;S&gt; DefaultInclude&lt;S&gt;(DbSet&lt;S&gt; set) where S : class
  {
    // base implementation just returns the set
    // derived versions would attach Includes to the set before returning it
    return set;
  }
  
  protected virtual DbSet&lt;S&gt; FullInclude&lt;S&gt;(DbSet&lt;S&gt; set) where S : class
  {
    // base implementation just returns the set
    // derived versions would attach Includes to the set before returning it
    return set;
  }
  </code></pre>
  
  <p>That is my base Repository, and I'd like to be able to override those <code>XyzInclude</code> methods in a derived class. But I need to override them with a non-generic implementation, and this obviously doesn't work at a language level.</p>
  
  <p>This was really easy with Linq2Sql in that I'd just configure a <code>DataLoadOptions</code> object and attach it to the context. With the <code>Include</code> API off of <code>DbSet</code> I'm stumped on how best to do this. I'm looking for recommendations on a simple implementation of a Strategy pattern or similar.</p>
  
  <p><strong>EDIT:</strong> I guess the essence of my question is really about overriding a generic method with a non-generic derived version. I'm looking for a pattern or technique that allows me to do that. Extension methods are one thing I'm looking at, but would prefer a more "pure" solution if anyone has one.</p>