Table Storage 2.0

This post has been a long time coming. In late October the Windows Azure Storage Team released a new version of the storage services API libraries. At first these libraries were shipped with the Azure SDK, then later provided by NuGet but used the same version number as the Azure SDK. Now even the version numbers are different, where the latest Azure SDK is 1.8 the storage API is on version 2.0.

You can still get the storage SDK on NuGet, or browse/fork the source code on GitHub.

Whats new in 2.0

New Dependencies: The previous version required a reference to the System.Data.Client assembly but now has dependencies on the ODataLib NuGet projects (Microsoft.Data.OData, Microsoft.Data.Edm, System.Spatial)

New Namespace: The namespace has changed from Microsoft.WindowsAzure.StorageClient to Microsoft.WindowsAzure.Storage and the different services are under .Blob .Queue .Table namespaces.

For more information checkout the overview, and migration guide articles.

Table Storage API in 2.0

Lets get down to it. You can continue to use the API in the standard way with context objects and LINQ but I wanted to show off the new object structure which improves on the clarity of the API.

CloudTable

Rather than instantiating a context class, you can just use the CloudTableClient similar to blobs and queues.

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable customers = tableClient.GetTableReference("customers");

Once you have a CloudTable object you can start creating objects and performing operations.

public class Customer:TableEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
}


Customer newCustomer = new Customer();
newCustomer.RowKey = "tylergd";
newCustomer.PartitionKey = "Manitoba";
newCustomer.FirstName = "Tyler";
newCustomer.LastName = "Doerksen";

TableOperation insertOp = TableOperation.Insert(newCustomer);
customers.Execute(insertOp);

TableOperation and TableBatchOperation

There are a number of static methods to create TableOperation objects like .Insert .Delete .Merge .Replace and a few more variations like .InsertOrMerge all the methods take an ITableEntity interface instance, which the TableEntity base class implements.

To me this is a crucial addition to the API because it allows the user to specifically define the storage operations, giving much more control than the previous data context abstraction.

If you want to perform batched operations you need only use a TableBatchOperation object.

TableBatchOperation batch = new TableBatchOperation();
batch.Add(TableOperation.Insert(newCustomer));
customers.ExecuteBatch(batch);

Or slightly cleaner…

TableBatchOperation batch = new TableBatchOperation();
batch.Insert(newCustomer);
customers.ExecuteBatch(batch);

This operation object structure is also used for read requests, which I will cover in a following post.

This is all the time I have right now. Please look out for the upcoming Table Storage 2.0 posts

  • Read Operations
  • Dynamic Model Objects
  • Building Advanced Queries

As always thanks for reading!

P.S. – Right now I am participating in a blog challenge with a few other western Canada software professionals. We have all entered a pool in which you are eliminated if you do not write a post every two weeks. So far everyone is still in and it has produced a number of excellent blog posts! Check out the links below.

One thought on “Table Storage 2.0”

Leave a Reply