16.12. Table Storage

Azure table storage is the third Azure storage option; it allows you to store .NET objects (entities in Azure terminology) and access them in a manner compatible with WCF Data Services. Azure table storage also requires that your entities have three additional fields:

  • PartitionKey

  • RowKey

  • TimeStamp

PartionKey and RowKey are combined as a composite key to uniquely identify a row, so it is important the combination of the two must be unique (Figure 16-20). The PartitionKey can be used by Azure to divide data onto different servers for load-balancing purposes, while TimeStamp is used for conflict resolution.

Figure 16.20. Visualization of an Azure table

I think it's fair to say that table storage is probably not the most intuitive technology ever invented, but it is very flexible and easy to use once you get over the initial "What the heck is this?" state. Items stored in table storage are created as standard .NET classes that inherit from TableServiceEntity. Another context class that inherits from TableServiceContext is also created that is used to interact with table storage.

This is actually simpler than it sounds, so let's create an example of table storage now to save and retrieve a Film entity:

  1. Create a new Cloud Service project called Chapter16.TableStorage, adding a single ASP.NET web role to it.

  2. In the WebRole1 project, add a reference to System.Data.Services.Client.

  3. Open the ServiceDefinition.csdef file, and add the following entry to the ConfigurationSettings section:

    <Setting name="DataConnectionString" />

  4. Open ServiceConfiguration.cscfg, and add the following entry to ConfigurationSettings:

    <Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />

  5. Add a new class to the project called AzureDataServiceContext, and add the following using directives:

    using Microsoft.WindowsAzure.StorageClient;
    using Microsoft.WindowsAzure;

  6. Now enter the following code in AzureDataServiceContext.cs:

    public class AzureDataServiceContext : TableServiceContext
    {
        public AzureDataServiceContext(string baseAddress,
                                       StorageCredentials credentials)
                : base(baseAddress, credentials)
        {
        }
    
        public IQueryable<Film> FilmTable
        {
            get
            {
                return this.CreateQuery<Film>("Films");
            }
        }
    }

  7. Add a new class to the project called Film with the following using directive:

    using Microsoft.WindowsAzure.StorageClient;

  8. Now add the following code to define the class:

    public class Film : TableServiceEntity
    {
        public int FilmID { get; set; }
        public string Title {get; set; }
    }

  9. Open Default.aspx.cs, and add the following using statements:

    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.ServiceRuntime;
    using Microsoft.WindowsAzure.StorageClient;

  10. Now enter the following code:

    CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
    {
        // Provide the configSetter with the initial value
        configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
    });
    
    var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
    var tableStorage = storageAccount.CreateCloudTableClient();
    tableStorage.CreateTableIfNotExist("Films");

    AzureDataServiceContext svc =
      new AzureDataServiceContext(storageAccount.TableEndpoint.ToString(),
                                  storageAccount.Credentials);
    
    Film f1 = new Film();
    f1.FilmID = 777;
    f1.Title = "Commando";
    f1.Timestamp = System.DateTime.Now;
    f1.PartitionKey = "UK";
    f1.RowKey = Guid.NewGuid().ToString();
    
    svc.AddObject("Films", f1);
    svc.SaveChanges();
    Film film = svc.FilmTable.Where(f => f.FilmID == 777).FirstOrDefault();
    
    Response.Write(film.Title);

  11. Press F5 to run your application, and you should see "Commando" printed out to the screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.85.221