title: Should repositories expose IQueryable to service layer or perform filtering in the implementation?
body: <p>I'm trying to decide on the best pattern for data access in my MVC application.
  Currently, having followed the MVC storefront series, I am using repositories, exposing IQueryable to a service layer, which then applies filters. Initially I have been using LINQtoSQL e.g.</p>
  
  <pre><code>public interface IMyRepository
  {
    IQueryable&lt;MyClass&gt; GetAll();
  }
  </code></pre>
  
  <p>Implemented in:</p>
  
  <pre><code>public class LINQtoSQLRepository : IMyRepository
  {
     public IQueryable&lt;MyClass&gt; GetAll()
     {
        return from table in dbContext.table
               select new MyClass
               {
                  Field1 = table.field1,
                  ... etc.
               }
     }
  }
  </code></pre>
  
  <p>Filter for IDs:</p>
  
  <pre><code>public static class TableFilters 
  {       
     public static MyClass WithID(this IQueryable&lt;MyClass&gt; qry, string id)
     {
        return (from t in qry
                where t.ID == id
                select t).SingleOrDefault();
     }     
  }
  </code></pre>
  
  <p>Called from service:</p>
  
  <pre><code>public class TableService
  {
     public MyClass RecordsByID(string id)
     {
        return _repository.GetAll()
                          .WithID(id);
     }
  }
  </code></pre>
  
  <p>I ran into a problem when I experimented with implementing the repository using Entity Framework with LINQ to Entities. The filters class in my project contains some more complex operations than the "WHERE ... == ..." in the example above, which I believe require different implementations depending on the LINQ provider. Specifically I have a requirement to perform a SQL "WHERE ... IN ..." clause. I am able to implement this in the filter class using:</p>
  
  <pre><code>string[] aParams = // array of IDs
  qry = qry.Where(t =&gt; aParams.Contains(t.ID));
  </code></pre>
  
  <p>However, in order to perform this against Entity Framework, I need to provide a solution such as the <a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0" rel="noreferrer">BuildContainsExpression</a> which is tied to the Entity Framework. This means I have to have 2 different implementations of this particular filter, depending on the underlying provider.</p>
  
  <p>I'd appreciate any advice on how I should proceed from here.
  It seemed to me that exposing an IQueryable from my repository, would allow me to perform filters on it regardless of the underlying provider, enabling me to switch between providers if and when required. However the problem I describe above makes me think I should be performing all my filtering within the repositories and returning IEnumerable, IList or single classes.</p>
  
  <p>Many thanks,
  Matt</p>