views:

6809

answers:

7

In wcf, what is the difference between applying the DataMember attribute on a property

private int m_SomeValue;

[DataMember]  
public int SomeValue {
  get {...}
  set {...}
}

instead of a member variable

[DataMember]  
private int m_SomeValue;

public int SomeValue {
  get {...}
  set {...}
}

?

+3  A: 

In theory, and as long as you keep m_SomeValue always equal to SomeValue (like a simple getter/setter), nothing. Other than the name of the variable exposed by the WCF. (Obviously, if you tag the m_ variable, then your proxy class will also have the same m_ name. The proxy class will generate a public property whether you use a public/protected/internal/private field or property.

However, if you have any special logic in your accessors that may modify the value returned (ToUpper()ing a string, for example), then you would return a different value.

Jarrett Meyer
+6  A: 

As long as you use the Name marker, the contract is identical regardless of whether the field or property is used.

[DataMember(Name="SomeValue")]
private int m_SomeValue;

However, there may be some permissions issues accessing private members, in particular on silverlight and CF - in which case I would recommend using the public property as the data-member. Actually, I would tend to always use a property unless I had a very good reason...

Marc Gravell
+10  A: 

In general, you should favor applying the DataMember attribute on the property, rather than on the private field. The only reason to apply the attribute to the field instead is if the property were read-only (i.e. it has no setter).

dthrasher
Why do we need to do that though?
Krishna
All serialization in WCF is two-way by default. This means that the framework needs to be able to read and write all of your data elements. So if you are modifying an existing type with read-only properties to be serializable, you might have to add the attributes to a field. This is a rare case, though.
dthrasher
It's also useful to put the DataMemberAttribute on a field if your property's getter/setter has any code that might alter its value.
rossisdead
+1  A: 

Personally I would just use the property and completely removed the member variable all together. i.e.

[DataMember]
public int SomeValue
{ get; set; }

The property will inexplicably create a member variable behind the scenes.

bj
"inexplicably"? That's what it's meant to do. Perhaps you meant "implicitly"?
John Saunders
How do it know?
Will
The C# compiler generates a private "backing" field with a mangled name (to prevent colliding with the name of any other field you may have defined) for the auto-property.
Kit
A: 

I think he meant automagically...

Alex G
A: 

If add [DataMember] on private int m_SomeValue, this member can not be serialization,so must be add it on public int SomeValue.

[DataMember]
private int m_SomeValue;

public int SomeValue { get {...} set {...} }

the code of above can not be get value in the client if you use it through WCF.

shawdren
DataContractSerializers will serialize any private field that has the DataMemberAttribute on it.
rossisdead
+1  A: 

There are good reasons you may want to mark fields rather than properties as DataMember .

Please check this for more details : http://blog.walteralmeida.com/2010/05/wcf-and-datacontract-serialization-internals-and-tips-.html

By the way: ContractSerializers will serialize any private field that has the DataMemberAttribute on it only if running in full trust environment. Does not work in partial trust (check the blog listed above for a solution)

Walter Almeida