user2783193
user2783193

Reputation: 1022

Proper way of decorating dto class in wcf communication

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.

Upvotes: 5

Views: 1338

Answers (2)

Piotr Czarnecki
Piotr Czarnecki

Reputation: 1688

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.

Upvotes: 3

nvoigt
nvoigt

Reputation: 77334

The following class is a simple DTO:

[DataContract]
public class DataTransferObjectExample
{
    [DataMember]
    public string ExampleData { get; set; }
}

Everything else would be for special cases that we can only check if you post your whole code.

Upvotes: 2

Related Questions