How to Defer Commands


  • Defer allows you to register server commands via the session.

  • All the deferred requests will be stored in the session and sent to the server in a single batch when SaveChanges is called, along with any other changes/operations made on the session.
    Thus, all deferred commands are executed as part of the session's SaveChanges transaction.

  • In this page:


Defer commands example

// Defer is available in the session's Advanced methods 
session.Advanced.Defer(
    
    // Define commands to be executed:
    // i.e. Put a new document
    new PutCommandData("products/999-A", null, new DynamicJsonValue
        {
            ["Name"] = "My Product",
            ["Supplier"] = "suppliers/1-A",
            ["@metadata"] = new DynamicJsonValue
            {
                ["@collection"] = "Products"
            }
        }),
    
    // Patch document
    new PatchCommandData("products/999-A", null, new PatchRequest
            {
                Script = "this.Supplier = 'suppliers/2-A';"
            },
            null),
    
    // Force a revision to be created
    new ForceRevisionCommandData("products/999-A"),
    
    // Delete a document
    new DeleteCommandData("products/1-A", null)
);

// All deferred commands will be sent to the server upon calling SaveChanges
session.SaveChanges();

Commands that can be deferred

The following commands implement the ICommandData interface and can be deferred:

Syntax

void Defer(ICommandData command, params ICommandData[] commands);
void Defer(ICommandData[] commands);
Parameter Type Description
command A command that implements the ICommandData interface The command to be executed
commands ICommandData[] Array of commands to be executed