DataMember最佳实践

原文:DataMember Best Practices

作者:Nicholas Allen

 

  1. 将 DataMember attribute 应用于属性,而不是相应的字段。
  2. 不要使用类型继承来更改数据协定,而是修改现有的类型或者创建一个全新的类型。
  3. 不要添加或删除版本之间的枚举成员。
  4. 不要更改数据协定或者版本间数据成员的名称、命名空间。
  5. 不要更改版本间数据成员的顺序。
  6. 在更高版本中新加的数据成员上将 IsRequired property 设置为 false.
  7. 不要再更高版本中删除数据成员,即使已经不再需要它们。
  8. 使数据协定实现 IExtensibleDataObject 接口,从而可以通过扩展类型来支持未来的兼容性。
  9. 仅仅在公共成员上使用 DataMember attribute. 对于部分信任环境来说私有数据成员的序列化是不被支持的。
  10. 对部分信任的调用者来说,不用企图通过构造函数的可见性、链接要求、安全操作或者构造函数中的验证检查来使得一个 DataContract 标识的类型安全。在初始化一个对象时 DataContractSerializer 不会调用构造函数。

PS: 附原文

I was asked to share a list of best practices I wrote for data contracts and data members so here it is:

  1. Do apply the DataMember attribute only to properties rather than the corresponding fields.
  2. Do not use type inheritance to version a data contract. Either modify the existing type or create an entirely new type.
  3. Do not add or remove enumeration members between versions.
  4. Do not change the name or namespace of a data contract or data members between versions.
  5. Do not change the order of data members between versions.
  6. Do set the IsRequired property to false on data members added in later versions.
  7. Do not remove data members in later versions even if they are marked as not required.
  8. Do support IExtensibleDataObject on data contracts to support future compatibility with extended types.
  9. Do apply the DataMember attribute only to public members. Serialization of private data members is not supported for partial trust environments.
  10. Do not rely on constructor visibility, constructor link demands, constructor security actions, or validation checks in the constructor to make a DataContract type safe for partially trusted callers. DataContractSerializer does not invoke the constructor when initializing an object.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值