WCF Data Services uses Data Service Providers to determine the shape and actual values of the data to expose. There are two providers implemented already for you (Entity Framework provider and Reflection provider) and you can also implement a custom provider.

Alex James is writing a great series about how to implement such custom provider. You should read it to understand how the custom provider works.

Custom Data Service Provider gives you the ability to define the metadata, the update behavior and lot of other extensibility points. The portion I will focus on in this series is the ability to provide custom query provider.

Whenever Data Services need to query the data source to get some data values, it uses the query provider. The most important method of that provider is the IDataServiceQueryProvider.GetQueryRootForResourceSet. This method returns IQueryable which Data Services uses to construct and execute the query.

There are many descriptions of how to implement IQueryable interface, so I won’t try to produce yet another one. I will just point you to the one written by Matt Warren here. You should read the first few posts of that series to understand the basics of IQueryable and expression trees.

In short IQueryable is an interface that allows the caller to construct a query and then run it. The caller (in our case Data Services) builds the query by constructing an expression tree which describes the intent of the query.

Implementing your IQueryable and supporting all the expression trees it can get in full is hard. In fact it’s so hard that I would say there’s only one complete implementation, LINQ to Objects. All other LINQ providers have some limitations. It’s so hard because the space of possible expression trees is infinite and very complex.

The good news for you, who I hope is trying to implement a Data Service Provider, is that Data Services will use only a very small portion of all possible Expression trees. The goal of this series will be to describe this small portion, so that you can implement your own IQueryable which will support enough expressions for Data Services to work correctly.

There’s even better news which is that depending on your requirements and features of Data Services you use, you can implement only part of the possible expression trees and still work correctly.

In this series we will take some portion or feature of the query language Data Services support, for example the $filter query option, and then we’ll describe what type of expression trees Data Services generates to describe the query to the underlying Data Service Provider.

In the next part we’ll talk about the query root and the expressions used by Data Services to access property values.

Data Services Expressions Series

This series describes expressions generated by WCF Data Services

Part 1 – Intro

Part 2 – The query root

Part 3 – Filters

Part 4 – Accessing properties

Part 5 – Sorting

Part 6 – Key lookups

Part 7 – Navigation

Part 8 – Projections

Part 9 – Expansions