views:

2352

answers:

5

I'm using t4 for generating repositories for LINQ to Entities entities.

The repository contains (amongst other things) a List method suitable for paging. The documentation for Supported and Unsupported Methods does not mention it, but you can't "call" Skip on a unordered IQueryable. It will raise the following exception:

System.NotSupportedException: The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'..

I solved it by allowing to define a default sorting via a partial method. But i'm having problems checking if the expression three indeed contains an OrderBy.

I've reduced the problem to as less code as possible:

public partial class Repository
{
    partial void ProvideDefaultSorting(ref IQueryable<Category> currentQuery);

    public IQueryable<Category> List(int startIndex, int count)
    {
     IQueryable<Category> query = List();
     ProvideDefaultSorting(ref query);
     if (!IsSorted(query))
     {
      query = query.OrderBy(c => c.CategoryID);
     }
     return query.Skip(startIndex).Take(count);
    }
    public IQueryable<Category> List(string sortExpression, int startIndex, int count)
    {
           return List(sortExpression).Skip(startIndex).Take(count);
    }
    public IQueryable<Category> List(string sortExpression)
    {
     return AddSortingToTheExpressionTree(List(), sortExpression);
    }
    public IQueryable<Category> List()
    {
           NorthwindEntities ent = new NorthwindEntities();
           return ent.Categories;
    }

    private Boolean IsSorted(IQueryable<Category> query)
    {
     return query is IOrderedQueryable<Category>; 
    }
}

public partial class Repository
{
    partial void ProvideDefaultSorting(ref IQueryable<Category> currentQuery)
    {
     currentQuery = currentQuery.Where(c => c.CategoryName.Contains(" ")); // no sorting..
    }
}

This is not my real implementation!

But my question is, how could I implement the IsSorted method? The problem is that LINQ to Entities query's are allways of the type ObjectQuery, which implements IOrderedQueryable.

So how should I make sure an OrderBy method is present in the expression tree? Is the only option to parse the tree?

Update
I've added two other overloads to make clear that it's not about how to add sorting support to the repository, but how to check if the ProvideDefaultSorting partial method has indeed added a OrderBy to the expression tree.

The problem is, the first partial class is generate by a template and the implementation of the second part of the partial class is made by a team member at a other time. You can compare it with the way the .NET Entity Framework generates the EntityContext, it allows extension points for other developers. So I want to try to make it robust and not crash when the ProvideDefaultSorting is not implemented correctly.

So maybe the question is more, how can I confirm that the ProvideDefaultSorting did indeed add sorting to the expression tree.

Update 2
The new question was answered, and accepted, I think I should change the title to match the question more. Or should I leave the current title because it will lead people with the same problem to this solution?

+1  A: 

I'm afraid it's a bit harder than that. You see, the Entity Framework will, in certain circumstances, silently ignore an OrderBy. So it isn't enough to just look for an OrderBy in the expression tree. The OrderBy has to be in the "right" place, and the definition of the "right" place is an implementation detail of the Entity Framework.

As you may have guessed by now, I'm in the same place as you are; I'm using the entity repository pattern and doing a Take/Skip on the presentation layer. The solution I have used, which is perhaps not ideal, but good enough for what I'm doing, is to not do any ordering until the last possible moment, to ensure that the OrderBy is always the last thing into the expression tree. So any action which is going to do a Take/Skip (directly or indirectly) inserts an OrderBy first. The code is structured such that this can only happen once.

Craig Stuntz
okay, thats a bummer. So if I understand correctly you move the logic about if it's sorted or not to the presentation layer? I can't say that sounds like an ideal solution.
Davy Landman
Because we allow users to dynamically resort data, having sorting on the presentation layer makes sense to us. But I won't claim it's appropriate for all applications. Do it where it makes sense to you, but make sure it's the last thing you do before Take/Skip, and it is only done once.
Craig Stuntz
I understand that you allow users to dynamically resort your data (for instance a gridview). One of my overloaded List method has a String sortExpression parameter which is parsed and converted to a LINQ expression. Maybe the problem is actually how to enforce the default sorting.
Davy Landman
We have the repository specify a default sort expression. If the user does not supply a sort expression of their own, the presentation layer can use the default from the repository. Remember that a sort is only required if there will be a Take/Skip. We don't want to require it otherwise.
Craig Stuntz
I upvoted it although it's not an answer, but I think it's important to note that the OrderBy is ignored when not in the correct place (when you think about its kinda logical).
Davy Landman
A: 

Paging depends on Ordering in a strong way. Why not tightly couple the operations? Here's one way to do that:

Support objects

public interface IOrderByExpression<T>
{
  ApplyOrdering(ref IQueryable<T> query);
}

public class OrderByExpression<T, U> : IOrderByExpression<T>
{
  public IQueryable<T> ApplyOrderBy(ref IQueryable<T> query)
  {
    query = query.OrderBy(exp);
  }
  //TODO OrderByDescending, ThenBy, ThenByDescending methods.

  private Expression<Func<T, U>> exp = null;

  //TODO bool descending?
  public OrderByExpression (Expression<Func<T, U>> myExpression)
  {
    exp = myExpression;
  }
}

The method under discussion:

public IQueryable<Category> List(int startIndex, int count, IOrderByExpression<Category> ordering)
{
    NorthwindEntities ent = new NorthwindEntities();
    IQueryable<Category> query = ent.Categories;
    if (ordering == null)
    {
      ordering = new OrderByExpression<Category, int>(c => c.CategoryID)
    }
    ordering.ApplyOrdering(ref query);

    return query.Skip(startIndex).Take(count);
}

Some time later, calling the method:

var query = List(20, 20, new OrderByExpression<Category, string>(c => c.CategoryName));
David B
Although this is a nice way to offer a sorting parameter, my interface already includes a overload which provides sorting. I will change my question to make it more clear.
Davy Landman
A: 
    ProvideDefaultSorting(ref query);
    if (!IsSorted(query))
    {
            query = query.OrderBy(c => c.CategoryID);
    }

Change to:

    //apply a default ordering
    query = query.OrderBy(c => c.CategoryID);
    //add to the ordering
    ProvideDefaultSorting(ref query);

It's not a perfect solution.

It doesn't solve the "filter in the ordering function" problem you've stated. It does solve "I forgot to implement ordering" or "I choose not to order".

I tested this solution in LinqToSql:

    public void OrderManyTimes()
    {
        DataClasses1DataContext myDC = new DataClasses1DataContext();
        var query = myDC.Customers.OrderBy(c => c.Field3);
        query = query.OrderBy(c => c.Field2);
        query = query.OrderBy(c => c.Field1);

        Console.WriteLine(myDC.GetCommand(query).CommandText);

    }

Generates (note the reverse order of orderings):

SELECT Field1, Field2, Field3
FROM [dbo].[Customers] AS [t0]
ORDER BY [t0].[Field1], [t0].[Field2], [t0].[Field3]
David B
This would indeed work, and at the moment I do have this as the temporary solution (to not break the build). But it's not the kind of solution I was looking for.
Davy Landman
+1  A: 

You can address this in the return type of ProvideDefaultSorting. This code does not build:

    public IOrderedQueryable<int> GetOrderedQueryable()
    {
        IQueryable<int> myInts = new List<int>() { 3, 4, 1, 2 }.AsQueryable<int>();
        return myInts.Where(i => i == 2);
    }

This code builds, but is insidious and the coder gets what they deserve.

    public IOrderedQueryable<int> GetOrderedQueryable()
    {
        IQueryable<int> myInts = new List<int>() { 3, 4, 1, 2 }.AsQueryable<int>();
        return myInts.Where(i => i == 2) as IOrderedQueryable<int>;
    }


Same story with ref (this does not build):

    public void GetOrderedQueryable(ref IOrderedQueryable<int> query)
    {
        query = query.Where(i => i == 2);
    }
David B
Thanks for your time, this did solve one problem, *IF* the partial method is implemented, I can at least be sure it's sorted. Then the only case remains, how to see if the partial method is implemented or not.
Davy Landman
Don't need to - just do the ID sorting, and then hand the IOrderedQueryable to the partial method.
David B
That could be a possibility, but I'd like to keep the query as clean as possible.
Davy Landman
I've accepted this as the answer, although it's a partial answer it did help me complete the whole picture (I posted that solution below).
Davy Landman
+1  A: 

Thanks to David B I've got a the following solution. (I had to add detection for the situation where the partial method was not executed or just returned it's parameter).

public partial class Repository
{
    partial void ProvideDefaultSorting(ref IOrderedQueryable<Category> currentQuery);

    public IQueryable<Category> List(int startIndex, int count)
    {
     NorthwindEntities ent = new NorthwindEntities();
     IOrderedQueryable<Category> query = ent.CategorySet;
     var oldQuery = query;
     ProvideDefaultSorting(ref query);
     if (oldQuery.Equals(query)) // the partial method did nothing with the query, or just didn't exist
     {
      query = query.OrderBy(c => c.CategoryID);
     }
     return query.Skip(startIndex).Take(count);
    }
    // the rest..        
}

public partial class Repository
{
    partial void ProvideDefaultSorting(ref IOrderedQueryable<Category> currentQuery)
    {
     currentQuery = currentQuery.Where(c => c.CategoryName.Contains(" ")).OrderBy(c => c.CategoryName); // compile time forced sotring
    }
}

It ensures at compile time that if the partial method is implemented, it should at least keep it an IOrderdQueryable.

And when the partial method is not implemented or just returns its parameter, the query will not be changed, and that will use the fallback sort.

Davy Landman