快捷搜索:

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 IMyContract

{...}

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 DataSets and DataTables (or their type-safe subclasses) from operations. Return a neutral representation such as an array.

不要吸收或者返回ADO.NET DataSets和DataTables (或它们的类型安然的子类)。返回一其中立的数据形式,比如数组。

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.

在持久化办事里,平日指定一个完成操作。

您可能还会对下面的文章感兴趣: