Azure SDK 2.0

With relase of the new 1.8 Azure SDK release, came version 2.0 of the .NET storage client.

A complete list of changes are available, but the main difference is an entirely new table storage client. This article covers what I’ve found out after a quick play.

Gone is the linq support, and instead is an API that makes the developer think about reading and writing entities in the most efficient way.

To get started, use the nuget package:

Install-Package WindowsAzure.Storage

The new SDK affords quite a bit of flexibility, but the easiest way to start is to create classes for each table, and inherit from TableEntity. TableEntity has the PartitionKey, RowKey, ETag etc.. properties, so you just need to define your own (Bar and Baz in this case).

public class Foo : TableEntity
{
    public string Bar { get; set; }
    public string Baz { get; set; }
}

To get a reference to your table:

var account = CloudStorageAccount.Parse("xxx");
var tableClient = account.CreateCloudTableClient();
var table = tableClient.GetTableReference("foo");
table.CreateIfNotExists();

To write an entity to your table:

var foo = new Foo
{
  RowKey = "1",
  PartitionKey = "1",
  Bar = "Hello World"
};
table.Execute(TableOperation.Insert(foo));

To read from the table, use the execute function again:

TableResult result = table.Execute(TableOperation.Retrieve<Foo>("1", "1"));
Foo foo = result.Result as Foo; // this will be null if it doesn't exist

There are a number of table operations, for merge, replace, delete etc… This is great, as it makes the developer acutely aware of exactly what will happen to the entity when it’s saved. It’s also obvious how to batch operations together (although there’s only one operation in this case!)

var batch = new TableBatchOperation();
batch.Add(TableOperation.Delete(foo));
table.ExecuteBatch(batch);

Use the TableQuery class to execute queries on other columns, but be careful, these may not be indexed:

var query = new TableQuery();
query.Where(TableQuery.GenerateFilterCondition("Bar", "eq", "Hello World"));
Foo foo = table.ExecuteQuery(query).First();

A TableQuery can also be used to limit which columns are returned, or just take a count.

If you don’t specify the type, you get a DynamicTableEntity, which has a dictionary of properties:

var query = new TableQuery();
var results = table.ExecuteQuery(query);

In summary, this looks like a well thought out library. The linq support in the old SDK hid quite a bit of complexity which, whilst being powerful, displaced the developer from the underlying features of the system. The new library only feels slightly closer to the metal, but gives me much more confidence about the code I write.

Advertisements