Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using POCOs when persisting to Azure Table Storage

I'm planning to use Azure Table Storage in my ASP.NET 5 (MVC 6) app and have added the WindowsAzure.Storage NuGet package, but I got really disappointed when I noticed that all my entnty models need to inherit from Microsoft.WindowsAzure.Storage.Table.TableEntity. Now I'm thinking the best solution is to have 2 sets of entities and create mappings between my main domain objects and the entity objects used to persist to Table Storage. I don't want to add the WindowsAzure.Storage package to all my projects.

The deprecated azure-sdk-for-net got support for POCOs at one point, but I don't see this in the current WindowsAzure.Storage.

What's the best practice here?

like image 643
henningst Avatar asked Nov 25 '15 09:11

henningst


People also ask

Is Azure table storage persistent?

Certain features of NServiceBus require persistence to permanently store data. Among them are subscription storage, sagas, and outbox. Various storage options are available including Azure Table and Azure Cosmos DB Table API.

Is Azure table storage being deprecated?

Azure table storage is being deprecated in favor of Azure Cosmos DB. Azure Cosmos DB offers many advantages over Azure table storage, such as: -Azure Cosmos DB is scalable.

When should I use Azure table storage?

Azure tables are ideal for storing structured, non-relational data. Common uses of Table storage include: Storing TBs of structured data capable of serving web scale applications. Storing datasets that don't require complex joins, foreign keys, or stored procedures and can be denormalized for fast access.


1 Answers

You have not given much detail about the type of entities you try to write to Azure Table Storage however if your entities contain nested complex properties and if you want to write the entire object graph including the complex nested properties (which themselves may contain nested properties), none of these suggested solutions work.

I have come across a similar problem and have implemented a generic object flattener/recomposer API that will flatten your complex entities into flat EntityProperty dictionaries and make them writeable to Table Storage, in the form of DynamicTableEntity.

Same API will then recompose the entire complex object back from the EntityProperty dictionary of the DynamicTableEntity.

Have a look at: https://www.nuget.org/packages/ObjectFlattenerRecomposer/

I am working with Azure team to integrate this API into Azure Storage SDK. You can have a look at the pull request and the code here:

https://github.com/Azure/azure-storage-net/pull/337/commits

Usage:

//Flatten object of type Order) and convert it to EntityProperty Dictionary
 Dictionary<string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(order);

// Create a DynamicTableEntity and set its PK and RK
DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(partitionKey, rowKey);
dynamicTableEntity.Properties = flattenedProperties;

// Write the DynamicTableEntity to Azure Table Storage using client SDK

//Read the entity back from AzureTableStorage as DynamicTableEntity using the same PK and RK
DynamicTableEntity entity = [Read from Azure using the PK and RK];

//Convert the DynamicTableEntity back to original complex object.
 Order order = EntityPropertyConverter.ConvertBack<Order>(entity.Properties);

That's all :)

Latest version of the nuget package also supports IEnumerable, ICollection etc. type properties as well.

The .Net Core version of the package is here: https://www.nuget.org/packages/ObjectFlattenerRecomposer.Core/

CosmosDb Table api version of the package is here: https://www.nuget.org/packages/ObjectFlattenerRecomposer.CosmosDb.Table.Core/

like image 147
Dogu Arslan Avatar answered Oct 27 '22 07:10

Dogu Arslan