Search code examples
wcfdatacontractdatamember

WCF DataMember attribute for read-only fields?


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

I cannot set the [DataMember] attribute on the public property since there is no set method, and I would like to keep it that way if possible since I do not want this value changed by external means. I cannot set the [DataMember] attribute on the private field since it throws 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 while going through the WCF server without making my property public?


Solution

  • Generally speaking, your data contract classes should be very lightweight data transfer objects without any logic or deeper meaning attached to them. Just containers to ferry data across the cloud. They should be public classes, with just a set of public read-write properties. In your business logic classes, you should convert these into some internal business entities and do the reverse when transmitting data.

    This separates the data transfer model from any internal entity model and ensures optimal maintainability, allowing you to avoid problems such as the one you are facing - where OO design issues conflict with WCF operating behavior.

    With very small projects the overhead of keeping a separate model might not be worth it. AutoMapper can be of some help minimizing the manual labor required.

    Speaking of your specific scenario, I am not sure I exactly understand the problem statement. You do not want some field to be modified? But this field is just a part of the data model - parts of the data model are never "modified" - there is no "old" data, just data your client makes up. Your client code just sends a data object to the server. If the server does not care about one member of the class, it should just ignore it.

    It's not like instnaces of the data contract objects exist on the server and wait for clients to manipulate them. A read-only field might conceptually make sense in such a scenario but this is not so with WCF. The client just makes up an 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 (if perhaps it is sometimes needed, only for specific users or such) make the server ignore it when it is not desired.