Zachary Scott
Zachary Scott

Reputation: 21182

Simple Repository Pattern Question: How to query across multiple repositories efficiently?

If you return IList(T) from your repository ...

How could you efficiently create SQL queries when you join the data together?

Is it REQUIRED to expose IQueryable / IEnumerable data structures for those methods? This to me is bad.

Or

Am I missing some basic concept?

Right now I have a repository methods like:

IList<T> Get( Expression(...) filter, Expression(...) sort, int skip, int take)

where null indicates Get All. This works very well until I want to find all Orders of a set of Customers without doing a query for each customer.

Upvotes: 1

Views: 878

Answers (1)

Craig Stuntz
Craig Stuntz

Reputation: 126547

It's not required to expose IQueryable<T>, but if you don't then you run into exactly the problem you're describing: You can't further compose the queries.

One solution is to put an entity service layer in front of the repository. The service layer is EF-ignorant, but can do LINQ projections. Projecting in LINQ results in a composed, single DB query.

I have a demo here.

Upvotes: 2

Related Questions