Enumerations & Data Contract

Topics: General Discussion Forum
Mar 8, 2007 at 7:43 PM
I was wondering,if any of you have added enumerations to data contracts and how this should be working in C#-code reusability context?

I added (see the below C# code) as additional class to data contracts project. Now I try to define another data contract (after compiled the solution) and reuse this as an type (by type selector) but I cannot select this class from type list, or this class is not showing up in selection list.

Any help on here are more than appreciated.

Alexander

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Qx.CServer.DataContracts
{
DataContract(Namespace = "http://qx.CServer.DataContracts/2007/03", Name = "Enumeration")
class Enumeration
{
enum ItemSouce
{
EnumMember(Value = "Participant")
Participant,
EnumMember(Value = "Admin")
Admin
}
}
}




Mar 8, 2007 at 8:00 PM
Edited Mar 8, 2007 at 8:03 PM
I found the way after looking some examples from MSDN, see below sample code (this can be picked by type selector) but now I have another question.

When using the enumeration in data contract do I have to implement a new data contract (that uses enumeration) from IExtensibleDataObject class as given in MSDN sample?

See the msdn article at http://msdn2.microsoft.com/en-us/library/system.runtime.serialization.enummemberattribute.aspx

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Qx.CServer.DataContracts
{
public enum MessageSouce
{
EnumMember(Value = "Participant")
Participant,
EnumMember(Value = "Admin")
Admin
}
}
Mar 8, 2007 at 8:13 PM
Edited Mar 8, 2007 at 8:15 PM
Seems like I am creating monolog discussion here. Anyhow would be great if someone would confirm that below is correct before I create more of these.

When data contract uses enumeration then data contract must be delivered from IExtensibleDataObject and data contract must implement ExtensionDataObject as given in sample.

According to MSDN article:

“The IExtensibleDataObject interface provides a single property that sets or returns a structure used to store data that is external to a data contract. The extra data is stored in an instance of the ExtensionDataObject class and accessed through the ExtensionData property. In a roundtrip operation where data is received, processed, and sent back, the extra data is sent back to the original sender intact. This is useful to store data received from future versions of the contract. If you do not implement the interface, any extra data is ignored and discarded during a roundtrip operation.”

Alexander
Developer
Mar 8, 2007 at 9:58 PM
As far as I know, the IExtensibleDataObject is better suited for versioning Best Practices: Data Contract Versioning and I may not use it in your scenario.
In your case, I would add the DataContract attribute to the enum type.
You can simply have something like this:

[DataContract]  // you may set the same namespace as your "main" contract class
public enum MessageSouce
{
   [EnumMember]
   Participant,
   [EnumMember]
   Admin
}
Then you may have your "main" contract class that use this enum like this:

[DataContract]
class Contact
{
   [DataMember]
   public string FirstName;
 
   [DataMember]
   public string LastName;
 
   [DataMember]
   public MessageSouce MessageSouce;
}
Charly
Mar 9, 2007 at 8:28 AM
Charly,

I wasnt sure if I need to implement this or not. But will go ahead and do as given in your code sample.

Thanks, Charly again...

Regards,
Alexander