Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure TableServiceEntity - storing complex classes

Storing data in Azure Table Services via TableServiceEntity you're limited to usual basic types (int, string, datetime etc) that have public get/set.

There is none of the usual magic you've come to expect from serialization that deals with collections, complex types, inheritance etc.

Different ways of dealing with this could be

  • Hooking into the WritingEntity and ReadingEntity events to manually set properties (inc dealing with complex types using some serialization method to plain string property).
  • Similar to above but use an additional 'storage class' to translate between YourClass <-> YourClassStorage <-> TableServices
  • Using a framework such as Lokad.Cloud's FatEntities or Lucifure

Have I missed anything? Which method may be best in which circumstances?

like image 407
Ryan Avatar asked Jul 04 '12 14:07

Ryan


2 Answers

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

There is at least one more way of dealing with the persistence of object trees and object relationships: Store related objects separately under a different PartitionKey/RowKey.

Simplest (brute force) way to implement that approach may require that you make multiple calls to ATS, so that you can deserialize objects properly.

If the number of transactions performed against storage is more important than the storage space and bandwidth used, a more elegant extension of that approach would be to implement interfaces for your entities and create a "wide" Union entity that implements all those interfaces - and that's the guy that is stored and retrieved. Each Union object that is retrieved is utilized through particular interface only. You can store collections as well as simply related entities this way - simply make sure your PartitionKey is the same for everyone who is related to the Union object and you have a way of identifying which Union object represents which entity type.

HTH

like image 169
Igorek Avatar answered Sep 28 '22 06:09

Igorek


I'm not sure if this answers your question, but if you need to upload complex types you could just serialize the data into a Byte Array.

I needed to upload a List of KeyValuePair's to TableStorage so I simply used a binary formatter to serialize it to a byte array, then deserialize and cast it back when retrieving it.

Serialize:

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

Deserialize and cast 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;
like image 45
Shahin Dohan Avatar answered Sep 28 '22 05:09

Shahin Dohan