aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Web/ExtensionMethods/CloudTableExtensions.cs
blob: ffffe69508e3f0ae2138ebaaa71df7ac792810df (plain)
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);
    }
}
>LOCALITY = "Local", POSTAL_CODE = "70800", STATE = "Hadarom", }, CONTACT = new remote.CONTACT() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow, EMAIL = "roy.mail.net@gmail.com", FAX = "1234", FIRST_NAME = "Roy", FULL_NAME = "Roy Ben Shabat", LAST_NAME = "Ben Shabat", PHONE_NUMBER = "1234" }, NAME = "Twine Demo", GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow, }; var configuration = new remote.CONFIGURATION() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow, APPLICATION_DISPLAY_PANEL_VERSIONS = new remote.APPLICATION_DISPLAY_PANEL_VERSIONS() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow }, APPLICATION_FIRMWARE_VERSIONS = new remote.APPLICATION_FIRMWARE_VERSIONS() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow }, APPLICATION_OS_VERSIONS = new remote.APPLICATION_OS_VERSIONS() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow }, APPLICATION_VERSIONS = new remote.APPLICATION_VERSIONS() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow }, CREATION_DATE = DateTime.UtcNow, EMBEDDED_FIRMWARE_VERSIONS = new remote.EMBEDDED_FIRMWARE_VERSIONS() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow }, EMBEDDED_SOFTWARE_VERSIONS = new remote.EMBEDDED_SOFTWARE_VERSIONS() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow }, HARDWARE_VERSIONS = new remote.HARDWARE_VERSIONS() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow }, }; var machine_version = new remote.MACHINE_VERSIONS() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow, VERSION = 1.0, }; var machine = new remote.MACHINE() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow, ORGANIZATION = organization, PRODUCTION_DATE = DateTime.UtcNow, SERIAL_NUMBER = "1234", MACHINE_VERSIONS = machine_version, }; var machine_configuration = new remote.MACHINES_CONFIGURATIONS() { GUID = Guid.NewGuid().ToString(), LAST_UPDATED = DateTime.UtcNow, CONFIGURATION = configuration, MACHINE = machine, }; remoteDB.MACHINES.Add(machine); remoteDB.MACHINES_CONFIGURATIONS.Add(machine_configuration); remoteDB.SaveChanges(); } } } }