Query Interceptor changes for ADO.NET Data Services
In the most recent release of ADO.NET Data Services, we changed the syntax of query interceptors to take no arguments and return a predicate expression which is applied as a filter.
So, for example, if your code used to read:
[QueryInterceptor("Customers")]
public IQueryable<Customer> QueryCustomers(IQueryable<Customer> q) {
return from c in q where c.IsPublic select c;
}
It should now be written as follows:
[QueryInterceptor("Customers")]
public Expression<Func<Customer, bool>> QueryCustomers() {
return c => c.IsPublic;
}
The return type of the method is a bit tricky, so let's break it up. First, it's an Expression of something, so it's not actually a delegate that we're going to invoke - instead, it's a representation of that code. The Expression is describing a function (thus the Func), which takes an input value (a Customer being evaluated) and returns a bool value (whether the Customer is passes the filter or not). Because we have an expression rather than a direct piece of code, we can actually pass this on to the query provider (which can generate T-SQL to run this on a database server, for example).
If you look at the C# documentation over at https://msdn.microsoft.com/en-us/library/bb397687.aspx, you'll see that the (x) => x * x syntax is a lambda expression, and you'll see two code samples early on: one creates a delegate that runs x * x, and the other creates an Expression object that represents the x * x code. The query interceptor code uses the second version, creating a specific type of Expression and returning it when invoked.
Why did we make this change? One of the main reasons is that this allows us to run query interceptors on $expand segments in addition to the regular paths. So for example if you query for /Customers?$expand=Orders, then any query interceptors on Orders will also run, giving a consistent view of the data in the service to its clients.
Hope this helped to clarify things - as usual, you can post questions here or in the forum at https://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=1430&SiteID=1.
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm.
Comments
- Anonymous
June 13, 2008
Marcleo Lopez Ruiz explains the changes to Astoria's Query Interceptors In the VS2008 SP1 Beta release