This project is read-only.

Exposing IQuerable as part of IRepository

Mar 30, 2009 at 2:55 PM

I'm trying to guess why IRepository interface exposes IQueryable, as this looks like exposing a leaky interface.
I understand that IRepository::Query() is returning an IEnumerable just to hide internal IQueryable details of each ORM, but at the same time IRepository<> itself exposes IQueryable as part of its definition.

What the reason behind such thing?
Mar 30, 2009 at 3:40 PM

The IRepository<T>.Query method takes in an impementation of a ISpecification instance. The Query method exists to allow you to query the repository using predefined specifications along with ad-hoc queries. For example:

var goldCustomersInAZ = from customer in customersRepository.Query(CustomersSpecifications.IsGoldStandardMember & CustomersSpecification.IsLongtimeCustomer)
                                         where customer.State == "AZ"
                                        select customer;

The above query allows you to query the repository, and the underlying store, using pre-defined specifications (or not, based on your implementation the specifications could be ad-hoc as well) and chain those with the query capabilities that IQueryable provides.

I hope the above clarifies the Query method.

Mar 30, 2009 at 7:12 PM
Edited Mar 30, 2009 at 7:12 PM
Well, my question was more about why exposing IQueryable as part of the IRepository<> interface.

I'm actually using the Query() method and find it really usefull as it allows me to define "what can be queried from within the repository" in form of Specification objects.

What I dont like (at least not initially) is exposing IQueryable directly, because IMHO it leaks Linq2Sql details to IRepository<> users. I find the specification as a much better abstraction.

So my question was.. Why deriving IRepository from IQuerable? This make the IQueryable interface available to any class accessing/using IRepository, which doesnt allows me to force people to use "IRepository::Query(..)" which is what I really want.