English 中文(简体)
WCF: Serialize complex objects with read-only members
原标题:

Looking for some guidance on a WCF service I’m prototyping.

I have a WCF service hosted in IIS that will pass data to my clients. I have a separate shared assembly that contains all my business objects that is referenced in my WCF project.

I want to have a few of the properties in these business objects read only as I don’t want my customers to be able to change these fields in their client code.

I read that decorating classes with the [DataContract] attribute enforces the proper serialization to maintain readonly fields but when I implement it the proxy classes generated in the client show as writable.

Are there any tricks to accomplish this?

Thanks!

/Eric

最佳回答

You can use regular properties, mark them with the DataMember attribute and make the set accessor private:

        [DataMember]
        public object IsFoo
        {
            get
            {
               return _isFoo;
            }
            private set { }
        }

EDIT: Also, to truly prevent users of your class from setting the property, you could always throw an InvalidOperation exception.

问题回答

In my experience, I have found that the WCF serializer works nicely with only very simple object models. If you planning on passing your business or domain objects over the wire, you may want to consider creating stand in "transfer" objects. This object will give you control over exactly what your consumers are receiving and can be mapped back to your domain objects.





相关问题
Choosing the right subclass to instantiate programmatically

Ok, the context is some serialization / deserialization code that will parse a byte stream into an object representation that s easier to work with (and vice-versa). Here s a simplified example ...

WCF Problem Sending Object To Client

Background Converting from using .Net Remoting to WCF. Most of the methods on the WCF server are working fine, but ran into one that isn t working today. This is the service contract: [...

WCF DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

deserialize the XML Document --- Need Help

I am using the below code snippet now to deserialize the XML document ... [WebMethod] public XmlDocument OrderDocument(XmlDocument xmlDoc) { XmlSerializer serializer = new XmlSerializer(typeof(...

How do I serialize a child class?

how do I include the serialized data from a child class where both impliment iserializeable? Class A Implements ISerializable dim _B as new B Class B Implements ISerializable ...

WCF: Serialize complex objects with read-only members

Looking for some guidance on a WCF service I’m prototyping. I have a WCF service hosted in IIS that will pass data to my clients. I have a separate shared assembly that contains all my business ...

Long/multiple SQL queries vs Serialization

I m trying to improve the performance of my web app where a page does a long query to pull data from different tables on a database. It pulls invoice data with multiple item lines, invoice status, and ...

热门标签