0

How should i be declaring the datacontracts

My Operation contract has a Method:

Apple GetApples()

My data Contract Apple looks Like

[DataContract]
public class Apple
{
   [DataMember]
   public int Id { get; set; }

    [DataMember]
    public FruitType type { get; set; }
}

As there is another member of type FruitType.

[DataContract]
public class FruitType
{
   [DataMember]
   public int Id { get; set; }

    [DataMember]
    public string type { get; set; }
}

OR as a simple class

  public class FruitType
  {     
    public int Id { get; set; }     
    public string type { get; set; }
  }

What is the difference between these two? other than that the simple type is not a datacontract and will depende on how i want to use it.?

how should i declare it??

fireholster
  • 622
  • 1
  • 9
  • 22
  • you should change the tag c to c# – deeiip Nov 19 '13 at 22:57
  • See this link. Your question is already answered [here][1] [1]: http://stackoverflow.com/questions/4836683/when-to-use-datacontract-and-datamember-attributes – deeiip Nov 19 '13 at 23:02

1 Answers1

0

Those attributes give you the control over how your properties will be represented in different formats. For example for XML you can specify the XML Namespace and XML node names.

Even if you are happy with default property names and default namespace, when you try to serialize data to XML, your XML nodes will have weird names such as typek_BackingField.

In other words, if you use WCF you should use DataContract and DataMember attributes, even if you think it works fine the formatted data may not look what you expect. As a result it removes compatibility with other (non-WCF) systems. Or even when you don't share your types (contracts) with other WCF systems.

evhen14
  • 1,839
  • 12
  • 16