Action Sequences in ADO.NET Data Services

October 31, 2008

Action Sequences in ADO.NET Data Services

In this post I’m Action Sequences in ADO.NET Data Services
going to demonstrate
how you can use the
action sequence feature
of the ADO.NET Data
Services AJAX Client Library

What is Action Sequence?

As written in my previous post, when we perform a CRUD operation using the
AJAX Client Library it is immediately transmitted and performed.
In the server side when using the DataServiceContext the operations are cached
and only transmitted when we use the SaveChanges method.
Action sequence enable us to perform the same unit of work functionality but on 
the client side. Using the ActionSequence object, which is part of the ADO.NET
Data Services AJAX Client Library
, we can cache the CRUD operations and submit
them together. An ActionSequence object is a wrapper object on top of a
Sys.Data.DataService object which collect the CRUD operations and uses the
DataService object it holds to submit them.

Creating Action Sequence

We have two ways to create an ActionSequence object:

  • Using the ActionSequence constructor:
  • var sequence = new Sys.Data.ActionSequence(proxy);

    Where the proxy is a Sys.Data.DataService object.

  • Using the Sys.Data.DataService’s createActionSequence method:
  • var sequence = proxy.createActionSequence();

One thing to know about the ActionSequence object is that once it was created
you can’t change the underling DataService object but you can access it through
the get_service property.
After we have the action sequence in hand it’s time to understand how to
register CRUD operations inside of it.

Registering CRUD Operations to a ActionSequence

The ActionSequence exposes three methods to register the three CRUD

  • Create – use the addInsertAction method:
  • sequence.addInsertAction(course, "Courses", context, true);

    The method takes four parameters:
    The object to insert, the resource set Uri, the context and a
    boolean to indicate if we want a return object.

  • Update – use the addUpdateAction method:
  • sequence.addUpdateAction(course, "Courses(1)", context, true);

    The method takes the same four parameters like the insert.

  • Delete – use the addRemoveAction method:
  • sequence.addRemoveAction(course, "Courses(1)", context);

    The method gets three parameters that are the same as the
    three parameters the previous methods got. The first two
    parameters are exclusively optional and you can use the first alone,
    the second alone or both of them.

Saving Changes Using an Action Sequence

After registering the operations to an ActionSequence object it’s time to
run them. The “magic” is performed using the executeActions method:

sequence.executeActions(OnSuccess, context);

The method gets two parameters – the callback function and a user context.
The actions will be performed one by one. The callback function will be called
after the sequence operations will complete, regardless of their success or
failure. That means that if some of the operations failed the sequence will
continue with the other operations. The callback function gets three
parameters – an array of ActionResult objects, a boolean that indicate
whether one of the operations in the sequence failed and a context.

Using the ActionResult Object

The ActionResult object is the representation of a result of an individual 
CRUD operation in the sequence. The array that is returned in the
executeActions method will hold the same number of ActionResult objects
as the number of the operations in the sequence. The ActionResult object
holds three properties – the result itself, the operation and the context of
the operation. The result property is the most interesting property. It can
hold the returned objects of insert or update operations if the boolean to
indicate if we want a return object was true. It can hold null otherwise
for insert or update. For delete operations it will always be null. If an error
occurred during the executing of an operation the result object will hold a
Sys.Data.DataServiceError that will indicate an error.

Cleaning the ActionSequence Object

After transmitting the operations as a unit of work using the action sequence
if we want to reuse the object we can call the clearActions method of the
object. The method will clear the sequence of all it’s operations.


Lets sum up, in the post I showed the how you can use the action sequence
feature of the ADO.NET Data Services AJAX Client Library. That feature enable
us to transmit a batch of operations to a data service from client side. This
feature and the ability to perform CRUD operations from client side makes
the data services world more flexible and versatile to use. 

    Add comment
    facebook linkedin twitter email

    Leave a Reply

    Your email address will not be published.

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>