WCF DataMember attribute for read-only fields?

I am trying to create a class with an explicit read-only field, however I am having problems storing the value when the object passes through the WCF server.

I cannot set the [DataMember] in the public property, since there is no set method, and I would like to save it this way if possible, since I do not want this value to be changed externally. I cannot set the [DataMember] in a private field, because it causes an error in partial trust environments.

 public class MyClass { private int _id; public int Id { get { return _id; } } private string _otherProperties; [DataMember] public string OtherProperties { get { return _otherProperties; } set { _otherProperties = value; } } } 

Is there a way to maintain the value of the Id field when passing through a WCF server without providing my property?

+7
wcf datacontract
source share
3 answers

Generally speaking, your data contract classes should be very lightweight data transfer objects without any logical or deeper values ​​associated with them. Just containers for sending data through the cloud. They should be public classes, just a set of public properties for reading and writing. In your business logic classes, you must convert them to some internal business objects and do the data backward transfer.

This separates the data transfer model from any internal entity model and ensures optimal maintainability, avoiding problems such as the one you encounter - where problems with OO design conflict with the working behavior of WCF.

With very small projects, the overhead of maintaining a separate model may not be worth it. AutoMapper can help minimize the amount of manual labor required.

Speaking about your specific scenario, I am not sure that I understand the problem statement exactly. Don't you want some field to be changed? But this field is only part of the data model - parts of the data model are never "modified" - there is no "old" data, but simply data that the client makes. The client code simply sends the data object to the server. If the server does not care about one member of the class, it should simply ignore it.

This is not like there are data contract objects on the server and are waiting for clients to manipulate them. A read-only field may conceptually make sense in such a scenario, but this is not the case with WCF. The client simply creates the object and sends it to the server. If you do not want the server to listen to some data, either do not add it to the data model, or (perhaps sometimes it is necessary, only for certain users or such) so that the server ignores it when it is not needed.

+7
source share

You can do it:

 public int Id { get; private set; } 

This will save the deserializer, preventing people from really setting the ID value. You will need to set it in the constructor or in the installer of another property.

However, I agree with Sanders that your DTO should be a dumb container.

+7
source share

Not. The data item must be able to generate getter and setter. The responsibility of the service to confirm that the identifier has not been changed on the client.

+1
source share

All Articles