Dynamic Table Storage

I maintain a project called AzureSugar, a library of extensions to the standard Windows Azure C# SDK which contains a number of handy methods for making the Azure Storage API easier to work with. I have just included support for using dynamic objects for table storage. As Table Storage does not enforce a fixed schema, it has always seemed a shame to me that the C# SDK forces you to use static types. Perhaps you don’t know the schema at compile time, perhaps you want to store a dictionary of items instead of a class, maybe you don’t want so much ceremony to just write a record to a table? The DynamicTableContext solves these problems.

To use it, first, create a context object:

var context = new DynamicTableContext("TableName", credentials); 

Then inserting a record is easy using a dynamic object for example:

context.Insert(new { PartitionKey="1", RowKey="1", Value1="Hello", Value2="World" }); 

You can do the same with a dictionary:

var dictionary = new Dictionary<string, object>(); 
dictionary["PartitionKey"] = "2"; 
dictionary["RowKey"] = "2"; 
dictionary["Value3"] = "FooBar"; 
context.Insert(dictionary); 

Retrieving an entity is striaght forward, just pass in the values for partition key and row key:

dynamic entity = content.Get("1", "1"); 

You can also pass in a query:

foreach (dynamic item in context.Query("Value1 eq 'Hello'")) 
{ 
  Console.WriteLine(item.RowKey); 
}

This is the first version, there are plenty of extra features I would like to add over time, but I hope there is enough here to be useful.

https://github.com/richorama/AzureSugar

Advertisement