I'm writing a layered ASP.Net Application which consist of Bussiness layer, Repository layer, service Layer... . In repository layer I'm using EntityFramework as ORM. In service layer, I want to pass a query in lambda form (which includes OrderBy or OrderByDescending ,take, skip,...) to repository layer and run the query on an DbSet and return result entities.
In Simple words : (How I can do something like the following mock code in asp.net c#)
public class Repository
{
public List<Book> findby(var query)
{
var dbcontext = DataContextFactory.GetDataContext();
//The following line should do this : dbcontext.Books.Where(B=>B.New==true && B.Id>99).OrderBy(B=>B.Date).ThenBy(B=>B.Id).Skip(2).Take(10);
List<Book> matchedBooks = RunQueryOnBooks(dbcontext.Books,query);
return matchedBooks;
}
}
public class Service
{
public List<Book> getTopNewBooks(Repository _repository)
{
var query = Where(B=>B.New==true && B.Id>99).OrderBy(B=>B.Date).ThenBy(B=>B.Id).Skip(2).Take(10);
List<Book> matchedBooks = _repository.findby(query);
return matchedBooks;
}
}
So the question is:
- which type I should use instead of var for query (If there is any and is possible)
- how I execute the query on dbcontext.Books ?
Please give a good and easy example like mine and more references. thanks in advance.