Skip to content

Updating a single object

Important

This operation updates a single object in the database. To update more than one object, use the bulkUpdate operation documented in the Updating multiple objects chapter of this guide.

To add this operation to a Unit Of Work instance, use the following methods:

The methods are organized by how the object to be updated is specified in the API. As you can see below, there are several ways to reference the object which will be updated in the database: explicit object reference, which is either using a Dictionary or a custom class or by using a result of a previous operation:

// Saves the object represented by the "objectToSave" argument in the database
// The object must have a server assigned value for the objectId property.
// The object will be saved in a data table with the "tableName" name.
// Values in the "objectToSave" dictionary will overwrite the ones in the 
database.unitOfWorkInstance.Update( String tableName, Dictionary<String, Object> objectToSave );
// Saves the object represented by the "objectToSave" argument in 
// the database. The YourCustomClass class must have either 
// the get/set methods for the objectId property or a public
// string field called objectId. The object must exist in a 
// data table which corresponds to the YourCustomClass class.
// For example if YourCustomClass is called Person, then the
// objectToSave must be stored in the Person table.
unitOfWorkInstance.Update( YourCustomClass objectToSave );
// Update an object represented by the "result" argument. The argument 
// is a result of a previous operation in the same transaction. 
// Since this operation expects a single object, the "result"
// argument must represent a single object as well. This means it
// can be a result from any of the following previous operations:
//   - saving a single object
//   - updating a single object
// The "changes" argument is a key/value dictionary of changes which will
// be updated in the object represented by "result". The keys in 
// "changes" correspond to the columns in the data table where 
// the updated object is stored.
OpResult unitOfWorkInstance.Update( OpResult result, 
                                    Dictionary<String, Object> changes );


// Same as the above, but instead of a collection/dictionary of "changes",
// this method accepts only one key/value pair in the form of
// propertyName and propertyValue. The propertyName argument
// is a property name in the object to be updated that will be
// updated with the value in the propertyValue argument.
OpResult unitOfWorkInstance.Update( OpResult result, 
                                    String propertyName, 
                                    Object propertyValue );


// Update an object represented by the "result" argument. The argument 
// is a reference to an object from a result of a previous operation
// in the same transaction. Since this operation expects a single object,
// the "result" argument must represent a single object as well.
// 
//  This means it can be obtained from any of the following previous 
// operations:
//   - saving multiple objects
//   - retrieving objects
// The "changes" argument is a key/value dictionary of changes which will
// be updated in the object represented by "result". The keys in 
// "changes" correspond to the columns in the data table where 
// the updated object is stored.
OpResult unitOfWorkInstance.Update( OpResultValueReference result, 
                                    Dictionary<String, Object> changes );


// Same as the above, but instead of a collection/dictionary of "changes",
// this method accepts only one key/value pair in the form of
// propertyName and propertyValue. The propertyName argument
// is a property name in the object to be updated that will be
// updated with the value in the propertyValue argument.
OpResult unitOfWorkInstance.Update( OpResultValueReference result, 
                                    String propertyName, 
                                    Object propertyValue );

Return Value

All methods listed above return an instance of the OpResult class which represents the updated object in the database. The OpResult object can be used as an argument in other operations in the same transaction (same UnitOfWork instance). For more information see the Operation Result chapter of this guide.

Example

The examples below demonstrate the usage of the operation for all possible ways to reference the object that should be updated. This is the what the data table looks like before the transaction runs:

data-before-update.zoom70

UnitOfWork unitOfWork = new UnitOfWork();
Dictionary<String, Object> changes = new Dictionary<String, Object>();
changes[ "objectId" ] = "37ADFFA8-39E2-73A5-FF44-651EF5C4E900";
changes[ "orderStatus" ] = "complete";
unitOfWork.Update( "Order", changes );

unitOfWork.Execute( new AsyncCallback<UnitOfWorkResult>(
callback =>
{
    //TRANSACTION COMPLETED
},
fault =>
{
    //SERVER REPORTED AN ERROR
} ) );

UnitOfWork unitOfWork = new UnitOfWork();

Order orderInstance = new Order();
orderInstance.objectId = "37ADFFA8-39E2-73A5-FF44-651EF5C4E900";
orderInstance.orderStatus = "complete";

unitOfWork.Update( orderInstance );

unitOfWork.Execute( new AsyncCallback<UnitOfWorkResult>(
callback =>
{
    //Transaction completed
},
fault =>
{
    //Server reported an error
} ) );
The Order class is defined as:
public class Order
{
   public String objectId;
   public String orderId;
   public String orderStatus;
}

UnitOfWork unitOfWork = new UnitOfWork();

// compose query builder
DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
// set the query condition
queryBuilder.SetWhereClause( "orderId = '031820-CV1'" );
// we need only one object, this will help to speed it up
queryBuilder.SetPageSize( 1 );
// add the find operation to the transaction
OpResult findResult = unitOfWork.Find( "Order", queryBuilder );

// get a reference to the first object (order) from the result
OpResultValueReference orderObjectRef = findResult.ResolveTo( 0 );

// compose a map of changes - notice the objectId is not needed there
Dictionary<String, Object> changes = new Dictionary<String, Object>();
changes[ "orderStatus" ] = "complete";

// add the update operation to the transaction
unitOfWork.Update( orderObjectRef, changes );

// run the transaction
unitOfWork.Execute( new AsyncCallback<UnitOfWorkResult>(
callback =>
{
  //transaction completed
},
fault =>
{
  //server reported an error
} ) );

This is what the same object in the data table looks like after the transaction runs:

data-after-update.zoom70