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?
Gets or sets a value that specifies whether to serialize the default value for a field or property being serialized.
A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.
If you prefer an "opt-in" approach, decorate the class with the DataContract attribute. If this attribute is present, members are ignored unless they have the DataMember. You can also use DataMember to serialize private members.
You must specify [DataMember] attribute on the property or the field of your Data Contract class to identify it as a Data Member. DataContractSerializer will serialize only those members, which are annotated by [DataMemeber] attribute.
You can do this:
public int Id
{
get;
private set;
}
This will keep the deserializer happy, while not letting people actually set the ID value. You'll have to set it in the constructor, or in the setter of another property.
However, I do agree with Sanders, in that your DTO should be a dumb container.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With