1

I am using Repository pattern and i have a base repository and i use Entity Framework and web api; my question is I want to allow my clients to be able to query any data dynamically; something like query expressions and fetch xml used in Dynamics CRM; I tried specification pattern but it isn't enough as i want to allow client code to order data with different columns e.g. Name asc Address desc and also, allow pagination on the returned result;so my method requirements are

  1. Generic Filter that doesn't depend on ORM technology as i may change Entity Framework in the future
  2. Generic Sorting methodology; allow multiple sort columns e.g. Name asc Address desc
  3. The Client determine the returned columns or all columns e.g. columnSet to be "Address, Name and Id" or returned the entire record
  4. Allow Pagination e.g. Page Index and Page Size
  5. Support threshold on the number of returned records as large result may affect performance

this is my initial method but i don't know if it is better or not

IList<TEntity> AllMatching(ISpecification<TEntity> specification = null,
                Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
                IList<Expression<Func<TEntity, object>>> includes = null,
                int? pageIndex = null, int? pageCount = null);

1 Answer 1

2

You can start doing this:

 public IQueryable<TEntity> Select(
 Expression<Func<TEntity, bool>> filter = null,
 Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
 IList<Expression<Func<TEntity, object>>> includes = null,
 int? page = null,
 int? pageSize = null)
 {
        IQueryable<TEntity> query = _dbSet;

        if (includes != null)
        {
            query = includes.Aggregate(query, (current, include) => current.Include(include));
        }
        if (orderBy != null)
        {
            query = orderBy(query);
        }
        if (filter != null)
        {
            query = query.AsExpandable().Where(filter);
        }
        if (page != null && pageSize != null)
        {
            query = query.Skip((page.Value - 1)*pageSize.Value).Take(pageSize.Value);
        }
        return query;
 }

As you can see, it almost do all that you need, so, if you want to do the same in your Repository implementation, you must use the LinqKit nuget package.

If you want to select specific columns , you can create another method as I show below:

IEnumerable<TResult> AllMatching<TResult>(
Expression<Func<TEntity, TResult>> columns,
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
IList<Expression<Func<TEntity, object>>> includes = null,
int? pageIndex = null,
int? pageCount = null)
{
  var query=Select(filter,orderby,includes,page,pageSize);
  return  query.Select(columns);

}
4
  • thanks for your help; i will try them but i want to sure if these queries are run on the database and not in memory; but does this solution support Generic Sorting methodology with multiple columns; please give me example on applying that on this solution;also how to apply threshold on the data returned i.e how to control the maximum number of returned rows
    – yo2011
    Commented Jun 25, 2015 at 8:07
  • also, the use of LinqKit is required
    – yo2011
    Commented Jun 25, 2015 at 8:20
  • Thanks it works fine without AsExpandable(); i don't know its benifit; also, i want to know how to set a threshold on the returned rows; all big applications such as SharePoint put a threshold on the executed query for performance issues
    – yo2011
    Commented Jun 25, 2015 at 23:39
  • Good to know, I use Linqkit because one day it thrown me an exception with some particular parameters, and as a suggestion to resolve that issue I found Linqkit. Unfortunately I don't know how to find and set a threshold to get the best performance in a query, maybe you can find more info about that subject on Internet. Sorry for not being more helpful
    – ocuenca
    Commented Jun 26, 2015 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.