DCSIMG
Batching in ADO.NET Data Services - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Batching in ADO.NET Data Services

Batching in ADO.NET Data Services

In the last post I’ve written Batching in ADO.NET Data Services
about performing CRUD
operations with the ADO.NET
data services
. In today’s post I’m
going to explain how to batch
these operations into one HTTP
request in order to increase
performance.

The Consuming Code

I’m using the same data service from my last post but I changed the
consuming code to the following:

   static void Main(string[] args)

   {

      // build the proxy

      var proxy = new CoursesDataContext(

         new Uri("http://localhost:4205/CoursesService.svc/"));

      proxy.MergeOption = MergeOption.AppendOnly;

 

      var fiveCreditCourses = from course in proxy.Courses

                              where course.Credit == 5

                              select course;

 

      var threeDaysCourses = from course in proxy.Courses

                             where course.Days == 3

                             select course;

 

      PrintCoursesToConsole(fiveCreditCourses);

      PrintCoursesToConsole(threeDaysCourses);

 

      Console.Read();

   }

 

   private static void PrintCoursesToConsole(IQueryable<Course> courses)

   {

      foreach (Course course in courses)

      {

         Console.WriteLine(

            "Course name: {0}, Course duration: {1}, Course credit: {2}",

            course.Title,

            course.Days,

            course.Credit

         );

      }

 

      Console.WriteLine();

   }

The Problem

When you’ll open the Fiddler tool to see the interaction you’ll
notice two HTTP requests, one for the five credit courses and the
second for the three days courses. The problem with that is
the number of round trips to the server in order to bring some pieces of
data. We would like to decrease the number of round trips in order to increase
the performance of the consumer.
For our rescue there is a feature of batching in ADO.NET data services.

Perform Batching in Data Service Consumer

ADO.NET data services has a feature of batching which enable us to send
batched HTTP request that will wrap a lot of operations (query or CRUD). The
main thing to know about the feature is that it’s enabled through a method
which is called ExecuteBatch. The method is located in the built proxy class. 
ExecuteBatch takes a bunch of DataServiceRequest class as parameter.
The DataServiceRequest represents a request objects that are submitted as part
of the batch operation. After building the batch request and executing it we’ll get
a DataServiceResponse object that is a collection of OperationResponse.
The OperationResponse is a response per operation that was sent in the batch
request.
So how the consuming code which I showed earlier will look like now?

   // build the proxy

   var proxy = new CoursesDataContext(

      new Uri("http://localhost:4205/CoursesService.svc/"));

   proxy.MergeOption = MergeOption.AppendOnly;

 

   var fiveCreditCourses = from course in proxy.Courses

                           where course.Credit == 5

                           select course;

 

   var fiveCreditCoursesUri =

      new Uri(fiveCreditCourses.ToString());

 

 

   var threeDaysCourses = from course in proxy.Courses

                          where course.Days == 3

                          select course;

 

   var threeDaysCoursesUri =

      new Uri(threeDaysCourses.ToString());

 

   var responses = proxy.ExecuteBatch(

      new DataServiceRequest<Course>(fiveCreditCoursesUri),

      new DataServiceRequest<Course>(threeDaysCoursesUri)

   );

 

   foreach (var response in responses)

   {

      QueryOperationResponse queryResponse =

         response as QueryOperationResponse;

 

      if (queryResponse != null)

      {

         foreach (var course in queryResponse.OfType<Course>())

         {

            Console.WriteLine(

               "Course name: {0}",

               course.Title

            );

         }

      }

 

      Console.WriteLine();

   }

 

   Console.Read();

Opening the Fiddler tool will reveal that only one HTTP request was sent
to the data service. Some things to notice about the example:

  • When building the URI’s I use the query ToString method to get
    the URI of the query. This can be changed in the future so be careful
    in using it. The other thing to do is to build the URI by yourself.
  • I cast the OperationResponse to QueryOperationResponse because
    I want to iterate the query results. There are places that you will not
    need to do the casting.

Using Batching in SaveChanges Method

When we use the SaveChanges method of the generated proxy every change
we made is transformed to an HTTP post request to the data service. In order
to send all the changes that where made until the SaveChanges was called in
one HTTP post request we can use the overload method of SaveChanges
that get a SaveChangesOptions enum as a parameter. In the SaveChangesOptions
choose the Batch option and all the changes will be submitted only once.

   proxy.SaveChanges(SaveChangesOptions.Batch);

Summary

Lets sum up the post, ADO.NET data services include a powerful batching
feature. The use of this feature can wrap a bunch of operations into only
one HTTP request and therefore can decrease the number of round trips
to the data service. On the other hand using it can increase to side of your
HTTP request and HTTP response so be careful in using the feature. In the
next post I’ll write about using concurrency in data services.

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# September 5, 2008 5:53 AM

alikl said:

good stuff!

thanks for sharing.

# September 5, 2008 2:41 PM

Gil Fink said:

Thanks Alik

# September 5, 2008 3:11 PM