Skip to main content

Session: How to defer operations?

Operations can be deferred till SaveChanges is called by using Defer method in Advanced session operations. There are four types of commands that can be deferred:

Syntax

// 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();
Parameters
ICommandData[]Array of commands implementing ICommandData interface.

Example

session
.Advanced
.Defer(
new PutCommandData("products/999-A", null, new DynamicJsonValue
{
["Name"] = "My Product",
["Supplier"] = "suppliers/999-A",
["@metadata"] = new DynamicJsonValue
{
["@collection"] = "Users"
}
}),
new PutCommandData("suppliers/999-A", null, new DynamicJsonValue
{
["Name"] = "My Product",
["Supplier"] = "suppliers/999-A",
["@metadata"] = new DynamicJsonValue
{
["@collection"] = "Suppliers"
}
}),
new DeleteCommandData("products/1-A", null)
);