Friday, October 03, 2008

ADO.NET Data Services (Project "Astoria") Query Interceptors

One of the interesting/handy features in ADO.NET Data Services is called query interceptors.  Query interceptors give you the ability to take action on the items returned from the service.

Each Query Interceptor is a method defined in your service and attributed with a QueryInterceptor interface.    The attribute takes an argument of the type of entity in which it is responsible for acting upon.  The results for all requests for this type of entity will then pass through the interceptor.

The interceptor is implemented as a lambda expression, using the Expression and Func types in C#.  An example of a Query Interceptor is below.  You see that it returns a generic Expression type with a specific type of Func, that takes an employee, and returns a boolean.

[QueryInterceptor("Employee")]
public Expression<Func<Employee, bool>> OnQueryEmployee()
{
 
    return pc => pc.Manager.LoginID.Equals("adventure-works\\"
                        + HttpContext.Current.User.Identity.Name) ||
        pc.LoginID.Equals("adventure-works\\"
            + HttpContext.Current.User.Identity.Name);
}

 

This is a pretty cool way of filtering/altering your results,  but what you have to keep in mind is that this expression is going to execute on each of the entities returned from the request.  I'll follow up with a post on another feature of ADO.NET Data Services called Service Operations that allows you to act upon a set of records.

0 comments: