Azure Table Storage with dynamic entities objects

In a recent project i use Azure Table Storage, but i need to keep the dynamic schema model that is present on the Cloud.

As you might know to use the Azure Table Storage you need to build a class that represent your table schema, a pattern similar to what is done with NHibernate,

In the local development table storage the API automaticaly creates the tables that represent the class you created, similar to what is done with NHibernate. But in the cloud the data is stored using Entity-Attribute-Value (EAV) Tables that means that there’s only one schema and is equal for all tables.

I want to keep this dynamic model and to continue to use the API (StorageClient) present on the AzureServicesKit, the only one that is supported by Microsoft.

So the solution that i found to acomplish this is to implement two events on the TableStorageDataServiceContext class:

  • ReadingEntity – Fired when the context is reading the Entity xml present on the cloud
  • WritingEntity – Fired when the context is about to write an entity to the cloud

And finnaly create a serializable class with a property bag that serializes respecting the schema of an Atom entry.

I implemented a class library in C#, the code is very easy to read and understand.

Download: http://cid-5d0350bbe4220916.skydrive.live.com/self.aspx/Public/DevScope.Cloud.StorageClient.zip?sa=734754923

Examples of usage:

EAVContext context = new EAVContext(); //this class inherits from TableStorageDataServiceContext

List<EAVEntry> entries = context.Entries("TableName").ToList();

EAVEntry entry = new EAVEntry();
entry.AtomUpdated = DateTime.Now;               
entry["TestProperty1"] = "Test";
entry["TestProperty2"] = 999;               
entry["TestDateProperty"] =  DateTime.Now.AddDays(20);

//insert
context.AddEntry(TableName, entry);

EAVEntry entry2 = context.Entries(TableName).Where(e => e.PartitionKey == entry.PartitionKey && e.RowKey == entry.RowKey).FirstOrDefault();

entry2["NewProp"] = DateTime.Now;
entry2["NewIntProperty"] = (entry.Properties.ContainsKey("NewIntProperty") ? Convert.ToInt32(entry["NewIntProperty"]) : 0) + 1;

//Update
context.UpdateEntry(entry2);

//Delete
context.DeleteEntry(entry2); 

Note: I dint find anywhere a class to serialize and deserialize a Atom Entry, só i created mine (EAVEntry) anyone know a better way?

Hope it helps

Advertisements

One thought on “Azure Table Storage with dynamic entities objects

  1. Pingback: Azure: Links, News and Resources (3) « Angel “Java” Lopez on Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s