Reputation: 13628
So i have been going through our code base and I have seen some our DTO's have a mix and match of [DataMember]
and [IgnoreDataMember]
attributes.
IN the past, we have been told that if we do not want something in the DTO serialised, simply do not add the [DataMember]
attribute. Then I saw the other attribute and did some digging and it seems that this explicitly states that the property will not be serialised.
Now my question is, which is better? Adding [IgnoreDataMember]
or not adding anything.
I have asked around and it seems that [IgnoreDataMember]
is from the days when everything was serialised and you had to dictate what should be ignored (I believe in .Net 2). Then they changed it to the reverse and you had to explicitly state what SHOULD be serialised. Now it seems that you can do both.
Upvotes: 39
Views: 35084
Reputation: 1063338
I have asked around and it seems that [IgnoreDataMember] is from the days when everything was serialised and you had to dictate what should be ignored (I believe in .Net 2). Then they changed it to the reverse and you had to explicitly state what SHOULD be serialised.
Actually that's not quite true; IIRC it has always been both:
[DataContract]
, then only the members marked [DataMember]
are considered[DataContract]
, then it defaults to everything, but you can subtract members using [IgnoreDataMember]
I usually just omit the [DataMember]
of things that I don't want serialized, but in many ways [IgnoreDataMember]
is more explicit - mainly for the benefit of the maintainer. It says "I am intentionally not serializing this", rather than "maybe I know that this isn't being serialized, but maybe I just forgot to add the attribute".
Either will work.
Upvotes: 69