insert complex objects to azure table with TableServiceEntity

后端 未结 2 612
面向向阳花
面向向阳花 2021-02-05 21:14

I was considering adding a whole complex object into a table. Having come from good old fashioned SQL approach I\'d obviously separate this into tables but I\'m trying a differe

相关标签:
2条回答
  • 2021-02-05 22:13

    Complex properties do not get saved into table storage. Only a subset of standard data types can be persisted. Check this link for more info on what data types are supported: http://msdn.microsoft.com/en-us/library/windowsazure/dd179338.aspx (Look under Property Types section)

    However, what you want to do is possible, just a little harder to implement. Here are two ways this can be done:

    • Expose CustomerSerialized, AddressSerialized and ItemsSerialized properties on your WholeObject class. Have these properties serialize/deserialize their associated complex objects to byte array, json or perhaps xml. Two downsides with this approach: a) you'll always be loading full object tree from the azure table storage even if you only need Customer object; b) you're limited to 64kb per property per object, so you better make sure that your InvoiceItems collection can fit into 64kb serialized

    • Come up with a PartitionKey/RowKey schema that would allow you to save all types of related objects as individual entities but with PartitionKey/Rowkey indicating their relationship. This would allow you to get at the full object tree or at the individual child objects independently. This approach does not have the two downsides of the first approach but it is definitely on the complex side of things when implementing. It also restricts your ability to tune PartitionKey/RowKey to other business needs. I've done this approach for a large e-commerce company with a great deal of success.
      In my project, The PartitionKey for all entities (root and child) was mapped to the ID of the "WholeObject" while RowKey consisted of the concatenated combination of ParentObjectID, ChildObjectID and ChildObjectType. When needing to retrieve all of the objects in the WHoleObject, simply issue a query against PartitionKey, otherwise use PartitionKey and part of RowKey to get at certain types of objects (ie: all of Addresses) or PartitionKey and full RowKey to get at an individual entity. This approach supported indefinitely deep object tree structure, however, once the number of entities went over 100, storing the object in one transaction was a headache.

    HTH

    0 讨论(0)
  • 2021-02-05 22:14

    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/

    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);
    

    Since then I worked with azure team and integrated this functionality to Azure Storage SDK version 8.0.

    One important note that api s in Azure SDK does NOT support IEnumerable or ICollection type properties. But if you get the nuget package v2 in the above link you can pretty much write any object into Azure Table Storage including objects with enumerable, collection type properties.

    https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.tableentity.flatten.aspx

    https://msdn.microsoft.com/en-us/library/azure/mt775432.aspx

    I also added TableEntityAdapter class to SDK version 8.2.0 to make the usage easier. Simply pass your complex entity to its constructor and it will transparently handle flattening/converting back your complex object on writes and reads. https://github.com/Azure/azure-storage-net/blob/master/Lib/Common/Table/TableEntityAdapter.cs

    0 讨论(0)
提交回复
热议问题