Azure Table Storage, WCF Service and Enum

后端 未结 6 973
故里飘歌
故里飘歌 2021-02-19 09:00

Here\'s my problem. A class which defines an order has a property called PaymentStatus, which is an enum defined like so:

    public en         


        
6条回答
  •  不知归路
    2021-02-19 09:34

    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.

    This is relevant to your question because the ObjectFlattenerRecomposer API supports flattening property types that are normally not writeable to Azure Table Storage like Enum, TimeSpan, all Nullable types, ulong and uint by converting them into writeable EntityProperties.

    The API also handles the conversion back to the original complex object from the flattened EntityProperty Dictionary. All that the client needs to do is to tell the API, I have this EntityProperty Dictionary that I just read from Azure Table (in the form of DynamicTableEntity.Properties), can you convert it to an object of this specific type. The API will recompose the full complex object with all of its properties including 'Enum' properties with their original correct values.

    All of this flattening and recomposing of the original object is done transparently to the client (user of the API). Client does not need to provide any schema or any knowledge to the ObjectFlattenerRecomposer API about the complex object that it wants to write, it just passes the object to the API as 'object' to flatten it. When converting it back, the client only needs to provide the actual type of object it wants the flattened EntityProperty Dictionary to be converted to. The generic ConvertBack method of the API will simply recompose the original object of Type T and return it to the client.

    See the usage example below. The objects do not need to implement any interface like 'ITableEntity' or inherit from a particular base class either. They do not need to provide a special set of constructors.

    Blog: https://doguarslan.wordpress.com/2016/02/03/writing-complex-objects-to-azure-table-storage/

    Nuget Package: https://www.nuget.org/packages/ObjectFlattenerRecomposer/

    Usage:

    //Flatten object (ie. of type Order) and convert it to EntityProperty Dictionary
     Dictionary 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(entity.Properties);
    

提交回复
热议问题