Why should repositories implemented using EF expose IQueryable rather than ObjectQuery?

273 Views Asked by At

Link:

My repositories always returns IQueryable. The reason for this is that IQueryable is not dependent on EF whereas ObjectQuery is. So if I want my other layers to be persistance ignorant I don't want to introduce dependency on ObjectQuery.

a) I assume reason why repositories ( implemented using EF ) should return IQueryable instead of ObjectQuery is because other ORMs also use queries which return IQueryable, and by having repositories return IQueryable we can easily switch between EF and other ORMs, without having to change the public interface of a repository?

b) Besides EF and NHibernate, are there any other ORMs that also have queries which return IQueryable?

Thank you

1

There are 1 best solutions below

0
Tim Lovell-Smith On

You should return IQueryable because that's the lingua franca of expression trees. If it's not IQueryable, it's either some other thing that understands Expression, or its some horrible custom language which gives you a greatly inferior programming experience to

var resultsINeed = getQueryable().Where(expression1).Select(expression2);

And if it understands the Expression example here, then it might as well just be IQueryable, because that's the whole point of IQueryable - to be that general abstraction, that everybody can reuse in their interfaces.