using Microsoft.WindowsAzure.Storage.Table; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public static class CloudTableExtensions { /// /// Demonstrate the most efficient storage query - the point query - where both partition key and row key are specified. /// /// Sample table name /// Partition key - i.e., last name /// Row key - i.e., first name /// A Task object public static T GetEntity(this CloudTable table, string partitionKey, string rowKey) where T : class, ITableEntity { TableOperation retrieveOperation = TableOperation.Retrieve(partitionKey, rowKey); TableResult result = table.Execute(retrieveOperation); T customer = result.Result as T; return customer; } /// /// The Table Service supports two main types of insert operations. /// 1. Insert - insert a new entity. If an entity already exists with the same PK + RK an exception will be thrown. /// 2. Replace - replace an existing entity. Replace an existing entity with a new entity. /// 3. Insert or Replace - insert the entity if the entity does not exist, or if the entity exists, replace the existing one. /// 4. Insert or Merge - insert the entity if the entity does not exist or, if the entity exists, merges the provided entity properties with the already existing ones. /// /// The sample table name /// The entity to insert or merge /// A Task object public static T InsertOrUpdateEntity(this CloudTable table, T entity) where T : class, ITableEntity { if (entity == null) { throw new ArgumentNullException("Specified entity is null."); } // Create the InsertOrReplace table operation TableOperation insertOrMergeOperation = TableOperation.InsertOrReplace(entity); // Execute the operation. TableResult result = table.Execute(insertOrMergeOperation); T insertedCustomer = result.Result as T; return insertedCustomer; } /// /// Delete an entity /// /// Sample table name /// Entity to delete /// A Task object public static void DeleteEntity(this CloudTable table, T deleteEntity) where T : class, ITableEntity { if (deleteEntity == null) { throw new ArgumentNullException("Specified entity was null."); } TableOperation deleteOperation = TableOperation.Delete(deleteEntity); table.Execute(deleteOperation); } }