title: How to not repeat data access rules in .NET MVC3 using EF (Repository pattern)?
body: <p>I have 2 classes with one-to-many like this:</p>
  
  <pre><code>public class Parent
  {
    public virtual List&lt;Child&gt; Children {get; set;}
  }
  </code></pre>
  
  <p>I am also using repository classes for each model, 
  ie: </p>
  
  <p>Parent has it's own repo class that has a Get() function that can gets an ordered list of Parents.</p>
  
  <p>Child also has it's own repo class tha thas a Get() function that can gets an ordered list of Children that have a status of Active.</p>
  
  <p>The problem is, the way EF works, since Parent has a reference to children, EF loads Children automatically when the parent list is loaded, but it gets active and inactive Children (also, the children list is not ordered). The check for active status is only in the Child repo.</p>
  
  <p>How can I limit my repetition of "get" rules to be used wherever Children are accessed.</p>
  
  <p>I know I can just modify the Parent repo to also check for Child status=Active, but then I am repeating my logic in every class that has children...</p>
  
  <p>Edit:
  I have a Repository class for Child that returns an Ordered list of Children:</p>
  
  <pre><code>    public List&lt;Children&gt; GetList()
      {
          IQueryable&lt;Children&gt; query = context.Set&lt;Children&gt;();
  
          return query.OrderBy(a=&gt;a.Seq).ToList();
      }
  </code></pre>
  
  <p>That works fine when I just need to access the Children directly. But when I need to get a Parent class, I use the repo function below:</p>
  
  <p>ParentRepo GetByID:</p>
  
  <pre><code>public Parent GetByID(object id)
      {
          IQueryable&lt;Parent&gt; query = context.Set&lt;Parent&gt;();
  
          Parent parentModel = context.Parents.Where(a =&gt; a.ParentId == (int)id)
              .Select(a =&gt; new
              {
                  Parent = a,
                  Children = a.Children.OrderBy(b =&gt; b.Seq)
              }).ToList()
              .Select(q =&gt; q.Parent)
              .Single();
  
          return parentModel;
      }
  </code></pre>
  
  <p>As you can see, I had to order the Children here as well (by Seq).</p>
  
  <p>This is the only way I can think to make this work, but it doesn't seem right.</p>
  
  <pre><code>public Parent GetByID(object id)
      {
          IQueryable&lt;Parent&gt; query = context.Set&lt;Parent&gt;();
  
          Parent parentModel = context.Parents.Single(a =&gt; a.ParentId == (int)id);
          parentModel.Children = childRepo.GetList(id);
          return parentModel;
      }
  </code></pre>