Deleting a single object¶
Important
This operation deletes a single object in the database. To delete more than one object, use the bulkDelete operation documented in the Deleting 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 deleted is identified:
// Delete the object represented as a Dictionary from the table with the name of tableName.
// The dictionaryBasedObject collection must have a key/value pair for the "objectId" key.
unitOfWorkInstance.Delete( String tableName, Dictionary<String, Object> dictionaryBasedObject );
// Delete the object represented as an instance of a custom class.
// Backendless deletes the object from a table with the same name
// as the name of the class. For example, if YourCustomClass is a
// class called Person, the the object will be deleted from the
// table Person. YourCustomClass must have public get/set method
// for the objectId property or a public objectId field.
unitOfWorkInstance.Delete( YourCustomClass classBasedObject );
// Delete an object identified by the objectId argument from the
// table with the name of tableName
unitOfWorkInstance.Delete( String tableName, String objectId );
// Delete an object which is a result of a prior operation in the
// same transaction. This operation expects a single object, which
// means the "result" argument can be the result from any of the
// following operations:
// - saving a single object
// - updating a single object
unitOfWorkInstance.Delete( OpResult result );
// Delete an object represented by a result from another operation
// in the same transaction. The difference between this method and
// the one above is the OpResultValueReference object represents
// a value within an OpResult object. For example, an OpResult may
// be a collection of objects from the "Retrieving objects" operation.
// A single object from that collection is represented by a
// OpResultValueReference instance used in this method. The
// "valueReference" argument can be an object from a result from any
// of the following operations:
// - retrieving objects
// - saving multiple objects
unitOfWorkInstance.Delete( OpResultValueReference valueReference );
Return Value¶
All methods return an instance of the OpResult
class which represents the timestamp when the object was deleted from 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 deleted:
// create unit of work - it represents the transaction
UnitOfWork unitOfWork = new UnitOfWork();
// create a dictionary-based object.
// the only required property is objectId.
// in your application the object may come from
// an API used outside of the transaction, or you
// can compose it the same way as shown below
Dictionary<String, Object> orderObject = new Dictionary<String, Object>();
orderObject[ "objectId" ] = "EB68A9E1-DC24-C961-FFC4-2EF9227BF700";
// add delete operation to the transaction
unitOfWork.Delete( "Order", orderObject );
// run the transaction
unitOfWork.Execute( new AsyncCallback<UnitOfWorkResult>(
callback =>
{
//Transaction completed
},
fault =>
{
//server reported an error
} ) );
UnitOfWork unitOfWork = new UnitOfWork();
// create a class-based object.
// the only required property is objectId.
// in your application the object may come from
// an API used outside of the transaction, or you
// can compose it the same way as shown below
Order order = new Order();
order.objectId = "996E2034-4CC6-0BC4-FF01-E92077819700";
// add Delete operation to the transaction
unitOfWork.Delete( order );
// run the transaction
unitOfWork.Execute( new AsyncCallback<UnitOfWorkResult>(
callback =>
{
//Transaction completed
},
fault =>
{
//server reported an error
} ) );
public class Order
{
public String objectId;
public String orderId;
public String orderStatus;
}
// create unit of work - it represents the transaction
UnitOfWork unitOfWork = new UnitOfWork();
// objectId of the object to delete.
String objectId = "37ADFFA8-39E2-73A5-FF44-651EF5C4E900";
// add Delete operation to the transaction
unitOfWork.Delete( "Order", objectId );
// run the transaction
unitOfWork.Execute( new AsyncCallback<UnitOfWorkResult>(
callback =>
{
//Transaction completed
},
fault =>
{
//server return an error
} ) );
// create unit of work - it represents the transaction
UnitOfWork unitOfWork = new UnitOfWork();
DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.SetWhereClause( "orderStatus = 'invalid'" );
OpResult invalidOrders = unitOfWork.Find( "Order", queryBuilder );
OpResultValueReference firstInvalidOrder = invalidOrders.ResolveTo( 0 );
// add delete operation to the transaction
unitOfWork.Delete( firstInvalidOrder );
// run the transaction
unitOfWork.Execute( new AsyncCallback<UnitOfWorkResult>(
callback =>
{
//Transaction completed
},
fault =>
{
//server reported an error
} ) );