Store .NET objects inside an Azure Table Storage and then retrieve them back in their original type
Non-relational databases are becoming more and more popular as a solution for storing your data. Cloud solutions like Microsoft Azure also get more popular with every day.
Currently Azure supports two types of “NoSQL” databases in the Azure portal. The one is DocumentDB (the NoSQL option in the main menu of the portal) and the other is the Table Storage (from the Storage accounts option in main menu). Do not confuse this Table with the tables of a relational database, they are not the same! A detailed analysis of the differences and similarities of the two technologies is beyond the purpose of this article, but we can summarize them to the following points:
DocumentDB can store complex structures of data like a list of objects or a list of lists with objects. On the other hand a Table Storage allows only key-value pairs of simple types (strings, numbers, booleans).
The DocumentDB-API supports more operations and query types with the database than a Table Storage. For querying a Table we have to make use of the Partition or Row Key.
Hosting a DocumentDB costs more money than hosting a Table Storage.
As one should expect from a NoSQL database, both options are very fast and are the optimal solution for use as data stores for huge amounts of information.
In order to be able to store complex object structures inside a Table Storage, we have to serialize these objects into a string. Does this process reminds you of something? Yes, JSON. Then we can deserialie the results of a query and we have again the complex objects in our C# code.
In the code that follows you see a working example. We first connect to the database, we add a new entity inside a Table and we then query this entity back into the C# environment. I have added several comments for each step of the process to make the code more understandable.
And here are the POCO classes. Remember that the class/ classes which we store in a Table Storage have to inherit from the TableEntity class:
Are you interested in the cloud technologies and especially in Azure? Drop me a line if you have any remark or question. I would be glad to help you.