ADO.NET Data Services 1.5: projections.

Recently I’ve started discussion about ADO.NET Data Services 1.5. I spoke about main trends in this project. Now I want to say about some details. Here we will speak about possibilities to create projections of data in version 1.5 of ADO.NET Data Services.

Necessity of creation of projections was present at first version of Astoria. However, as we know, time and resources are limited, therefore such possibility has appeared only in version 1.5. Possibility of creation of projections is necessary in those situations when an entity, containing in collections on service contain a considerable quantity of fields, and some fields are necessary to the client only. In this case the client is compelled to load all fields of these objects and by that to generate the superfluous traffic and increasing time of performance of queries.

In version 1.0 it was offered to solve a problem by division of the big entity on a little bit small and to work with them separately. Certainly, such approach creates many problems. For example, in such situation it is required to make additional efforts in case of editing of such collections. Besides, the quantity of collections of data on service can essentially increase. In this case the logical decision arises - to ask for service to return not all fields of objects, but only what it is necessary directly in URI.

The idea of work of the mechanism of projections consists in it in ADO.NET Data Services 1.5. Every time when it is necessary to return a specific set of fields, we set it obviously in URI. Thus other fields are ignored and not transferred to the client.

For execute of similar query there was a new "select" keyword. For example, if it is necessary to select two fields ProductName and UnitPrice it is necessary to execute the following query:

http://../WebDataService.svc/Products()?$select=ProductName,UnitPrice

Similarly, if it is necessary to select all fields, having ignored this operator or having specified as value "*":

http://../WebDataService.svc/Products()?$select=*

In some situations possibility of creation of projections can be undesirable. In this case it can be switch off, using additional parametres in service configuration. For this purpose in DataServiceConfiguration class there was property DataServiceBehavior which allows to adjust behaviour of service. In this case we are interested in AcceptProjectionRequests property.

public class WebDataService : DataService<NorthwindEntities>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
 
        config.DataServiceBehavior.AcceptProjectionRequests = true;
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

At use of a client proxy possibility of creation of projections also can be used conveniently. For this purpose it is necessary to construct LINQ-query where in Select section to specify necessary fields.

var client = new NorthwindEntities(new Uri(@"http://.../WebDataService.svc/"));
var q = from c in client.Categories
        select new { c.CategoryName, c.Description };

This functionality was very necessary in first version ADO.NET Data Services and now it was is added. And now we can use services even more effectively.

No Comments