Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Table Storage, WCF Service and Enum

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

    public enum PaymentStatuses : int
    {
        OnDelivery = 1,
        Paid = 2,
        Processed = 3,
        Cleared = 4
    }

And later on, in the class itself, the property definition is very simple:

    public PaymentStatuses? PaymentStatus { get; set; }

However, if I try to save an order to the Azure Table Storage, I get the following exception:

System.InvalidOperationException: The type Order+PaymentStatuses' has no settable properties.

At this point I thought using enum isn't possible, but a quick Google search returned this: http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/7eb1a2ca-6c1b-4440-b40e-012db98ccb0a

This page lists two answers, one of which seems to ignore the problems and suggests that using an enum in Azure Storage is fine.

Now, I don't NEED to store the enum in the Azure Table Storage as such, I could just as well store a corresponding int, however, I do need this property to be exposed in the WCF service.

I've tried making the property use get and set to return the enum from a stored integer, and remove this property from Azure by using the WritingEntity event on my DataContext, but I get that exception before the event for this entity is fired.

At this point, I'm at a loss, I don't know what else I can do to have this property in WCF as an enum, but have Azure store just the int.

like image 555
Shaamaan Avatar asked Feb 02 '11 16:02

Shaamaan


People also ask

Is Azure table storage being deprecated?

Azure table storage is being deprecated in favor of Azure Cosmos DB. Azure Cosmos DB offers many advantages over Azure table storage, such as: -Azure Cosmos DB is scalable.

What type of storage is Azure table storage?

Azure Table storage is a service that stores non-relational structured data (also known as structured NoSQL data) in the cloud, providing a key/attribute store with a schemaless design. Because Table storage is schemaless, it's easy to adapt your data as the needs of your application evolve.

What is ETag in Azure storage?

The ETag is used for concurrency. If you load a table entity and want to update it, you pass to update the ETag from when you loaded the value and it will update the entity only if that ETag matches what is stored.


2 Answers

Enum is not supported. Even though it is defined like an int, it is really not an 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 convert it using Enum.Parse.

like image 73
Scott Densmore Avatar answered Oct 24 '22 03:10

Scott Densmore


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.

like image 12
Ohad Schneider Avatar answered Oct 24 '22 04:10

Ohad Schneider