Azure Table Storage, WCF Service and Enum

后端 未结 6 969
故里飘歌
故里飘歌 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:17

    Just suggestions...

    I remember that in WCF you have to mark enums with special attributes: http://msdn.microsoft.com/en-us/library/aa347875.aspx

    Also, when you declare PaymentStatuses? PaymentStatus, you are declaring Nullable<PaymentStatuses> PaymentStatus. The ? sintax is just syntactic sugar. Try to remove the ? and see what happen (you could add a PaymentStatuses.NoSet = 0 , because the default value for an Int32 is 0).

    Good luck.

    0 讨论(0)
  • 2021-02-19 09:19

    Parvs solution put me on the right track but I had some minor adjustments.

    private string _EnumType;
    private EnumType _Type;
    
    //*********************************************
    //*********************************************
    public string EnumType
    {
        get { return _Type.ToString(); }
        set
            {
                _EnumType = value;
                try
                {
                    _Type = (EnumType)Enum.Parse(typeof(EnumType), value);     
                }
                catch (Exception)
                {
                    _EnumType = "Undefined";
                    _Type = [mynamespace].EnumType.Undefined;                  
                }                        
            }
        }
    
    0 讨论(0)
  • 2021-02-19 09:28

    Enum is not supported. Even though it is defined like an int, it is really not a integral type supported by Table Storage. Here is the list of types supported. An enum is just a string expression of an integral number with an object-oriented flavor.

    You can store int in table storage and then covert it using Enum.Parse.

    0 讨论(0)
  • 2021-02-19 09:28

    ya i was having this same problem i changed my property which was earlier enum to int. now this int property parses the incoming int and saves it into a variale of the same enum type so now the code that was

    public CompilerOutputTypes Type 
    {get; set;}
    

    is chaged to

    private CompilerOutputTypes type;
    public int Type 
    {
      get {return (int)type;}
      set { type = (CompilerOutputTypes)value; }
    }
    
    0 讨论(0)
  • 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<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);
    
    0 讨论(0)
  • 2021-02-19 09:38

    Here's a simple workaround:

    public int MyEnumValue { get; set; } //for use by the Azure client libraries only
    [IgnoreProperty] public MyEnum MyEnum
    {
        get { return (MyEnum) MyEnumValue; }
        set { MyEnumValue = (int) value; }
    }
    

    It would have been nicer if a simple backing value could have been employed rather than an additional (public!) property - without the hassle of overriding ReadEntity/WriteEntity of course. I opened a user voice ticket that would facilitate that, so you might want to upvote it.

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