Azure TableServiceEntity - storing complex classes

Saving data in Azure Table Services through TableServiceEntity is limited to the usual base types (int, string, datetime, etc.) that have a public get / set.

There is no ordinary magic that you expect from serialization that is associated with collections, complex types, inheritance, etc.

Different ways to deal with this can be

Did I miss something? Which method might be best in what circumstances?

+8
c # azure azure-table-storage
source share
2 answers

Inheritance is supported by ATS. All inherited properties in the implemented concrete class will be stored and retrieved. However, complex types are indeed not supported.

There is at least one other way to deal with preserving object trees and object relationships: Store related objects separately in another PartitionKey / RowKey section.

The simplest (rough) way to implement this approach may require that you make multiple ATS calls so that you can deserialize objects properly.

If the number of transactions performed in relation to the storage is more important than the used storage space and the used bandwidth, the more elegant extension of this approach will be the implementation of interfaces for your objects and the creation of a β€œwide” entity object that implements all these interfaces - and that guy, which is stored and retrieved. Each Union object that is retrieved is used only through a specific interface. You can store collections, as well as simply related objects this way - just make sure your PartitionKey is the same for everyone associated with the Union object, and you have a way to determine which Union object represents the entity type.

NTN

+5
source share

I'm not sure if this answers your question, but if you need to load complex types, you can just serialize the data into a byte array.

I needed to load the KeyValuePair list into a TableStorage, so I just used a binary formatter to serialize it to an array of bytes, then deserialize and discard it when retrieving.

Serialization:

MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, keyValuePairList); byte[] ba = ms.ToArray(); 

Disable and drop back:

 MemoryStream ms = new MemoryStream(byteArray); BinaryFormatter bf = new BinaryFormatter(); var obj = bf.Deserialize(ms); //cast object back to List of KeyValuePair var keyValuePairList = (List< KeyValuePair<string,string> >)obj; 
+3
source share

All Articles