1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
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
{
/// <summary>
/// Demonstrate the most efficient storage query - the point query - where both partition key and row key are specified.
/// </summary>
/// <param name="table">Sample table name</param>
/// <param name="partitionKey">Partition key - i.e., last name</param>
/// <param name="rowKey">Row key - i.e., first name</param>
/// <returns>A Task object</returns>
public static T GetEntity<T>(this CloudTable table, string partitionKey, string rowKey) where T : class, ITableEntity
{
TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
TableResult result = table.Execute(retrieveOperation);
T customer = result.Result as T;
return customer;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="table">The sample table name</param>
/// <param name="entity">The entity to insert or merge</param>
/// <returns>A Task object</returns>
public static T InsertOrUpdateEntity<T>(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;
}
/// <summary>
/// Delete an entity
/// </summary>
/// <param name="table">Sample table name</param>
/// <param name="deleteEntity">Entity to delete</param>
/// <returns>A Task object</returns>
public static void DeleteEntity<T>(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);
}
}
|