WCF服务编程设计规范(3):服务契约、数据契约和实例管理设计规范

  WCF服务编程设计规范(3):服务契约、数据契约和实例管理设计规范。本节涵盖服务契约和数据契约设计规范,以及服务实例管理内容。中英对照版本,欢迎留言交流。
Service Contracts
服务契约
1.        Always apply the  ServiceContract  attribute on an interface, not a class:
ServiceContract 属性 标记到契约接口上,而不是服务类上
  //Avoid: 避免
  [ServiceContract]
  class  MyService
  {
  [OperationContract]
  public void MyMethod()
  {...}
  }
  //Correct: 正确
  [ServiceContract]
  interface IMyContract
  {
  [OperationContract]
  void MyMethod();
  }
  class MyService : IMyContract
  {
  public void MyMethod()
  {...}
  }
2.        Prefix the service contract name with an  I :
服务契约名称以 I 开头
  [ServiceContract]
  interface  I MyContract
  {...}
3.        Avoid property-like operations:
避免定义与属性类似的操作
  //Avoid:
  [ServiceContract]
  interface IMyContract
  {
  [OperationContract]
  string GetName();
  [OperationContract]
  void SetName(string name);
  }
4.        Avoid contracts with one member.
避免契约里只包含一个成员
5.        Strive to have three to five members per service contract.
每个契约里尽量保证 3-5 个成员
6.        Do not have more than 20 members per service contract. Twelve is probably the  practical limit.
          每个服务契约里成员不要超过 20 个。 12 个也许久应该就是极限
Data Contracts
数据契约
1. Avoid inferred data contracts (POCO). Always be explicit and apply the  DataContract  attribute.
          避免使用推测性的数据契约。明确使用 DataContract 属性定义数据契约。
1.        Use the  DataMember  attribute only on properties or read-only public members.
只在属性或者只读的成员上使用 DataMember 属性
2.        Avoid explicit XML serialization on your own types.
避免自己的类型上明确使用 XML 序列化标记
3.        Avoid message contracts.
避免使用消息契约
5. When using the  Order  property, assign the same value to all members coming from  the same level in the class hierarchy.
          当使用 Order 属性的时候,对于类层次相同的所有成员赋相同的值
6. Support  IExtensibleDataObject  on your data contracts. Use explicit interface  implementation.
          数据契约支持 IExtensibleDataObject 。使用明确地实现接口。
7. Avoid setting  IgnoreExtensionDataObject  to  true  in the  ServiceBehavior  and  CallbackBehavior  attributes. Keep the default of  false .
          避免在 ServiceBehavior CallbackBehavior 属性里把 IgnoreExtensionDataObject  设置为 true 。保持默认的 false
8. Do not mark delegates and events as data members.
          不要使用委托和事件作为数据成员
9. Do not pass .NET-specific types, such as  Type , as operation parameters.
          不要传递 .NET-specific 类型,比如 Type ,作为操作参数。
10. Do not accept or return ADO.NET  DataSet s and  DataTable s (or their type-safe  subclasses) from operations. Return a neutral representation such as an array.
          不要接受或者返回 ADO.NET  DataSet s DataTable s ( 或它们的类型安全的子类 ) 。返回一个中立的数据形式,比如数组。
11. Suppress the generation of a generic type parameter hash code and provide a legible  type name instead.
          不要产生泛型类型参数的哈希值,使用一个易懂的类型名称作为替代。
Instance Management
实例管理
1. Prefer the per-call instance mode when scalability is a concern.
          当考虑到可伸缩性的时候,使用 Per_Call 模式,单调模式。
2. If setting  SessionMode.NotAllowed  on the contract, always configure the  service instancing mode as  InstanceContextMode.PerCall .
          如果在契约上设置了 SessionMode.NotAllowed ,通常会把服务实例模式设置为 InstanceContextMode.PerCall
3. Do not mix sessionful contracts and sessionless contracts in the same service.
          不要在一个服务里把会话契约和非会话契约混用。
4. Avoid a singleton unless you have a natural singleton.
          避免使用单例模式,除非理所当然地应该使用单例模式。
5. Use ordered delivery with a sessionful service.
          尽量在会话服务里使用顺序传递。
6. Avoid instance deactivation with a sessionful service.
          避免在会话服务里停止服务实例
7. Avoid demarcating operations.
          避免分布操作(比如有先后顺序的操作。)
8. With durable services, always designate a completing operation.
          在持久化服务里,通常指定一个完成操作。



 本文转自 frankxulei 51CTO博客,原文链接: http://blog.51cto.com/frankxulei/319610 ,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值