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.
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);
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;
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