Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way of decorating dto class in wcf communication

Tags:

c#

.net

wcf

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.

like image 543
user2783193 Avatar asked Nov 22 '13 12:11

user2783193


People also ask

What is DTO in WCF?

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.

What is DataContract and DataMember in c#?

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.

What is DataContract in WCF?

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.

Which classes are responsible for message serialization in WCF?

Use the DataContractSerializer with default settings for serialization.


1 Answers

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.

like image 186
Piotr Czarnecki Avatar answered Oct 12 '22 07:10

Piotr Czarnecki