views:

103

answers:

5

I'm trying to manually build a WCF Data Service using a POCO data model and I cannot figure out how to properly expose enum values. Assuming a simple model like:

public class Order
{
   public int ID {get; set;}
   public string Description {get; set;}
   public OrderStatus Status {get; set;}
}

public enum OrderStatus
{
   New,
   InProcess,
   Complete
}

How do you expose the valuable information in the OrderStatus property via the OData WCF Data Service?

If you do nothing, the Data Service generates a runtime error (enum is invalid property). The only answer I've seen that at least resolves the error is to mark the enum property as Ignored, such as:

[System.Data.Services.IgnoreProperties("Status")]
public class Order ...

This works, but it forces you to "omit" valuable information from the service layer. Are there other options for working with enum values in WCF Data Services?

EDIT: Please notice this is WCF Data Services (aka Astoria). This is not raw WCF Services, in which case the answers are more clear.

+1  A: 

You need to make the enum a Data Contract.

See here for an example: http://consultingblogs.emc.com/merrickchaffer/archive/2007/04/03/Passing-Enum-values-into-WCF-Service-operations.aspx

[Edit] Apparently, that's not always the case as seen here: http://stackoverflow.com/questions/187505/sharing-enum-with-wcf-service

Troy Guinn
Thanks for the quick input, but I think you missed that this is WCF Data Services, not plain WCF. I've tried the typical DataContract/EnumMember attributes, but they do not seem to work with WCF Data Services (aka Astoria).
Todd
+2  A: 

Assuming DataContract Serialization, like so:

[DataContract]
public class Order
{
   [DataMember]
   public int ID {get; set;}
   [DataMember]
   public string Description {get; set;}
   [DataMember]
   public OrderStatus Status {get; set;}
}

[DataContract]
public enum OrderStatus
{
    [EnumMember]
    New,
    [EnumMember]
    InProcess,
    [EnumMember]   
    Complete
}
nonnb
This generally works for plain WCF Services, but in this case I'm asking about WCF Data Services (formerly known as Astoria). In my tests, this solution DOES NOT work for using enums in Data Services. Have you tested this with WCF Data Services?
Todd
Oops - apols. There is a dirty hack in the links on Troy's post which might work for you - add an extra serializable int to the entity and then cast the enum to the getter / setters (but ignore the enum)
nonnb
+4  A: 

Enums are currently not supported in WCF Data Services (the OData protocol doesn't support them either). The typical workaround is to use string and constant values, or integer and constant values.

Vitek Karas MSFT
I was afraid of that. Happy to have clear confirmation at least. I'm trying some "wrapper" workarounds so I don't have to make large changes to my model just to use Data Services. Hopefully one of the workarounds will work!
Todd
+1  A: 

Maybe we can "cheat" it with the below workaround:

[System.Data.Services.IgnoreProperties("Status")]
public class Order
{
   public int ID {get; set;}
   public string Description {get; set;}
   public OrderStatus Status {get; set;}
   public int StatusValue
   {
      get
      {
           return (int)this.Status;
      }
      set
      {
          // Add validation here
          this.Status = (OrderStatus)value;
      } 
   }
}

public enum OrderStatus
{
   New,
   InProcess,
   Complete
}
Boris Modylevsky
Very near the final solution I used!
Todd
A: 

As follow-up, the "wrapper" approach is ultimately what worked. Essentially, a small class is written to wrap the enum and return primitive int values in the Data Service:

[IgnoreProperties("EnumValue")]
public class OrderStatusWrapper
{
    private OrderStatus _s;

    public int Value
    {
        get{ return (int)_t; }
        set { _t = (OrderStatus)value; }
    }

    public OrderStatus EnumValue
    {
        get { return _t; }
        set { _t = value; }
    }

    public static implicit operator OrderStatusWrapper(OrderStatus r)
    {
        return new OrderStatusWrapper { EnumValue = r };
    }

    public static implicit operator OrderStatus(OrderStatusWrapper rw)
    {
        if (rw == null)
            return OrderStatus.Unresolved;
        else
            return rw.EnumValue;
    }
}  

This was largely based on the advice given for working around EF4's enum limits:

http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4.aspx

Hope this technique helps others that follow.

Todd