@Alex - i know this is an old question, but what I would be doing would be letting the Repository do really simple stuff only. This means, get all records for a table or view.
Then, in the SERVICES layer (you are using an n-tiered solution, right? :) ) i would be handling all the 'special' query stuff there.
Ok, example time.
Repository Layer
ContactRepository.cs
public IQueryable<Contact> GetContacts()
{
return (from q in SqlContext.Contacts
select q).AsQueryable();
}
Nice and simple. SqlContext
is the instance of your EF Context
.. which has an Entity
on it called Contacts
.. which is basically your sql Contacts class.
This means, that method basically is doing: SELECT * FROM CONTACTS
... but it's not hitting the database with that query .. it's only a query right now.
Ok .. next layer.. KICK ... up we go (Inception anyone?)
Services Layer
ContactService.cs
public ICollection<Contact> FindContacts(string name)
{
return FindContacts(name, null)
}
public ICollection<Contact> FindContacts(string name, int? year)
{
IQueryable<Contact> query = _contactRepository.GetContacts();
if (!string.IsNullOrEmpty(name))
{
query = from q in query
where q.FirstName.StartsWith(name)
select q;
}
if (int.HasValue)
{
query = from q in query
where q.Birthday.Year <= year.Value
select q);
}
return (from q in query
select q).ToList();
}
Done.
So lets recap. First, we start our with a simple 'Get everything from contacts' query. Now, if we have a name provided, lets add a filter to filter all contacts by name. Next, if we have a year provided, then we filter the birthday by Year. Etc. Finally, we then hit the DB (with this modified query) and see what results we get back.
NOTES:-
- I've omitted any Dependency Injection for simplicity. It's more than highly recommended.
- This is all pseduo-code. Untested (against a compiler) but you get the idea ....
Takeaway points
- The Services layer handles all the smarts. That is where you decide what data you require.
- The Repository is a simple SELECT * FROM TABLE or a simple INSERT/UPDATE into TABLE.
Good luck :)