So, I've read all the Q&A's here on SO regarding the subject of whether or not to expose IQueryable to the rest of your project or not (see here, and here), and I've ultimately decided that I don't want to expose IQueryable to anything but my Model. Because IQueryable is tied to certain persistence implementations I don't like the idea of locking myself into this. Similarly, I'm not sure how good I feel about classes further down the call chain modifying the actual query that aren't in the repository.
So, does anyone have any suggestions for how to write a clean and concise Repository without doing this? One problem I see, is my Repository will blow up from a ton of methods for various things I need to filter my query off of.
Having a bunch of:
IEnumerable GetProductsSinceDate(DateTime date);
IEnumberable GetProductsByName(string name);
IEnumberable GetProductsByID(int ID);
If I was allowing IQueryable to be passed around I could easily have a generic repository that looked like:
public interface IRepository<T> where T : class
{
T GetById(int id);
IQueryable<T> GetAll();
void InsertOnSubmit(T entity);
void DeleteOnSubmit(T entity);
void SubmitChanges();
}
However, if you aren't using IQueryable then methods like GetAll() aren't really practical since lazy evaluation won't be taking place down the line. I don't want to return 10,000 records only to use 10 of them later.
What is the answer here? In Conery's MVC Storefront he created another layer called the "Service" layer which received IQueryable results from the respository and was responsible for applying various filters.
Is this what I should do, or something similar? Have my repository return IQueryable but restrict access to it by hiding it behind a bunch of filter classes like GetProductByName, which will return a concrete type like IList or IEnumerable?
I ended up creating two sets of methods, ones that return IEnumerable (in your case IQueryable), and ones that return Collection (pull the content before sending it out of the repository.)
This allows me to do both build ad hoc queries in Services outside the repository as well as use Repository methods directly returning side effect resistant Collections. In other words, joining two Repository Entities together results in one select query, instead of one select query for each entity found.
I imagine you could set your protection level to keep truly bad things from happening.
Rob's method really doesn't solve your core problem, and that is not wanting to write individual methods for each type of query you'd like to run, and unfortunately if you're not using IQueryable then that is what you are left with.
Sure the methods might be in the "service" layer, but it still means having to write "GetProductsByName, GetProductsByDate"...
The other method is something like:
This might give you some benefit over using IQueryable in that you can constrain what is returned.
hmm.. I solved this in many ways depending on the type of ORM i use.
The main idea is to have one repository base class and one query method that takes so many parameters indicating all possible where/orderby/expand|include/paging/etc options.
Here is a quick and dirty sample using LINQ to NHibernate (of course the entire repository should be implementation detail):
Normally you'll want to add many chaining overloads as shortcuts when you don't need paging for example, etc..
Here is another dirty one. Sorry I'm not sure if I can expose the final ones. Those were drafts and are OK to show:
In all cases nobody should be interacting with the repository directly except the services layer.
Most flexible thing is to let Services interact with Repository whatever way they want, same as in above code (yet through one single point -as in example also- to write DRY code and find a place for optimization).
However, the more right way in terms of common DDD patterns is to use the "Specification" pattern, where you encapsulate all your filters, etc in Variables (Class Members, in LINQ typically of delegate types). LINQ can take big optimization benefit out of this when you combine it with "Compiled queries". If you google the {Specification Pattern} and {LINQ Compiled Queries} you'll get closer to what I mean here.
Exposing an
IQueryable
is a very viable solution and this is how most of the Repository implementations out there doing right now. (Including SharpArchitecture and FubuMVC contrib, as well.)This is where you are wrong:
This is not realy true. Your example is correct and you should rename GetAll() to a more informative name.
It DOESN'T return all of the items if you call it. That is what IQueryable is for. The concept is called "deferred loading", as it only loads the data (and makes database requests) when you enumerate the
IQueryable
.So, let's say I have a method like this:
Then, I can call it like this:
This ONLY retrieves one row from the database.
And this:
This also generates a corresponding query and is only executed when you enumerate it. (It generates an expression tree from the query, and then the query provider should translate that into an appropriate query against the data source.)
You can read more about it in this MSDN article.
Having struggled to find a viable solution to this problem myself, there's what seems to be a good solution in the Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10) article.
The article doesn't talk about this exact issue but does talk about generic, reusable repository methods.
So far this is all I've been able to come up with as a solution.