I have class BookDTO which represents object which will be used in exchanging data between client and service where service is wcf service have following attributes
[Serializable]
[DataContract]
[KnownType(typeof(Book))]
public class BookDTO {...}
Is this proper (standard) way of decorating object which will be send over the wire? I've seen examples with
[DataContract(NameSpace="somenamespace.DTO.Book")]
Is [KnownType(typeof(Book))]
redudant here?
I've forget to mention that I'm introduced with DataMember attributes, so please disregard that.
DTO's are for sending data across layers and is independent of WCF. They're great in cases where you need to add more data members, as your interface is not broken and, assuming you're employing versioning, can access the new members in your updated methods.
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.
A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts.
Use the DataContractSerializer with default settings for serialization.
Actually when you use DataContract metatag, Serializable Metatag do nothing. This is a quote from MSDN article:
With [Serializable], all fields become part of the data contract (unless they are marked with [NonSerialized]). With [DataContract], only members marked with [DataMember] are included. Note that if a type has both [DataContract] and [Serializable] attributes on it, it will use the [DataContract] mapping
http://msdn.microsoft.com/en-us/magazine/cc163569.aspx
KnownType atrribute Marvin described to you in a comment to your question.
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