Search code examples
c#azureazure-table-storage

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?


Solution

  • 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