程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WCF服務編程設計規范(2):序言、通用設計規范和要點

WCF服務編程設計規范(2):序言、通用設計規范和要點

編輯:關於.NET

WCF服務編程設計規范(2):序言、通用設計規范和要點。主要翻譯整理,序言、WCF設計通用設計規范、設計要點。並給出注釋。注釋內容於括號內。下一部分會整理,服務契約、數據契約、實例管理的內容。這裡放出的是中英文對照版。方便大家學習。最後翻譯結束,我會整理一個純中文版放出。如有錯誤,請批評指正。歡迎留言交流。

序言

對於一次成功的產品交付來說,完整編碼規范至關重要。這個規范可以幫助強化一些通用的最佳實踐准則,並避免犯錯,方便整個團隊理解這些知識。通常,編碼規范都是相當的繁瑣,動辄上百頁,內容詳細敘述每個編碼規則的基本原理。雖然這比沒有規范要好的多,但是通常開發人員難以理解和掌握。相反,《WCF編碼規范》只有10多頁,並且詳細介紹“是什麼”和“為什麼”。我相信要完全理解每個編碼規則的精髓並恰當地應用這些規則,需要閱讀大量的書籍和多年的工作經驗。當有新人進入團隊時,你可以快速地告訴他或她應該“先看看這個規范”。因為,完全理解這些規范需要時間和今年感言,在此之前,我們就應該遵守它。這裡涵蓋了一些常用的規則、缺陷、指南和建議。這裡會使用在《WCF Master Class》和《Programming WCF Services》(精通WCF課程和WCF服務編程)裡介紹的最佳實踐和Helper Class。

Juval Lowy

General Design Guidelines

通用設計規范

1.All services must adhere to these principles:

所有的服務必須遵守這些原則:

a)Services are secure.

服務是安全的。

b)Service operations leave the system in a consistent state.

服務操作保證系統在一直的狀態。

c)Services are thread-safe and can be accessed by concurrent clients.

服務是線程安全的並且可以被並發的客戶端訪問。

d)Services are reliable.

服務是可靠的。

e)Services are robust.

服務是健壯的。

2.Services can optionally adhere to these principles:

這些原則不是必須的。

a)Services are interoperable.

服務是可互操作的

b)Services are scale-invariant.

服務是可以伸縮的

c)Services are available.

服務是可用的

d)Services are responsive.

服務是可以相應的

e)Services are disciplined and do not block their clients for long.

服務是遵守規則的,不能長時間阻塞客戶端進程。

Essentials

設計要點

1.Place service code in a class library, not in any hosting EXE.

服務類應該定義在類庫而不是exe宿主裡。

2.Do not provide parameterized constructors to a service class, unless it is a singleton

that is hosted explicitly.

不要給服務類提供參數化的構造函數,除非其是singleton單例模式,並且確定宿主。

3.Enable reliability in the relevant bindings.

在相關的綁定裡啟用可靠性。

4.Provide a meaningful namespace for contracts.For outward-facing services, use your company’s URL or equivalent URN with a year and month to support versioning.

For example:

為契約定義有意義的命名空間。暴露的外部服務,使用公司的URL或URN,並附上年月日等支持版本化。

例如:

[ServiceContract(Namespace = "http://www.idesign.net/2009/06")]

interface IMyContract

{...}

For intranet services, use any meaningful unique name, such as MyApplication.

對於企業內部服務,使用唯一的有意義的名字,比如MyApplication。

For example:

例如:

[ServiceContract(Namespace = "MyApplication")]

interface IMyContract

{...}

5.With intranet applications on prefer self-hosting to IIS hosting when the WAS is unavailable.

對於企業內部應用系統,當WAS不可用的時候,推薦自托管self-hosting而不是IIS。

6.Do not mix and match named bindings with default bindings.Either have all your bindings be explicitly referenced, or use only default bindings.

不要混用指定綁定和默認綁定。要麼全部明確指定綁定,要麼只使用默認綁定。

7.Do not mix and match named behaviors with default behaviors.Either have all your behaviors be explicitly referenced, or use only default behaviors.

不要混用指定行為和默認行為,要麼全部明確執行行為,要麼使用默認行為。

8.Always name all endpoints in the client config file.

在客戶端的配置文件裡,定義終結點的名字name.

9.Do not use SvcUtil or Visual Studio 2010 to generate a config file.

不要使用SvcUtil工具或者Visual Studio 2010去生成配置文件。(這裡工具會使用很多默認的設置,這些會帶來潛在的問題,在產品部署以後,比如TimeOut和MessageMaxSize等,最好自己能手動更改這些配置)

10.When using a tool such as Visual Studio 2010 to generate the proxy, do clean up the proxy.

當使用Visual Studio 2010生成客戶端代理的時候,記得釋放代理。(這裡應該是重寫IDispose的Dispose方法,釋放非托管資源。Proxy默認是繼)

11.Do not duplicate proxy code.If two or more clients use the same contract, factor the  proxy to a separate class library.

不要復制代理代碼。如果兩個或多個客戶端使用了相同的契約,把代理分解到獨立的類庫裡。(這裡不建議復制Proxy代碼的方式。如果你有多個客戶端,比如A 是一個Console程序,B是一個WindowsService程序,C是一個WinForm程序,都調用了相同的WCF服務,可以把這個WCF客戶端放在一個類庫裡DLL,其它項目都引用即可)

12.Always close or dispose of the proxy.

通常要關閉或者銷毀(close或dispose proxy)代理實例。(客戶端代理包含網絡連接等珍貴資源,繼續釋放,調用Close方法或者在Using體力聲明客戶端代理即可)

13.When using discovery, prefer dynamic addresses.

當使用服務發現機制的時候,推薦動態地址。(動態定位服務地址是WCF服務發現的優勢)

14.When using discovery, do support the metadata exchange endpoint over TCP.

當使用服務發現機制的時候,記得暴露支持TCP元數據交換的終結點。(這裡指的是Ad-Hoc模式,適用於企業局域網,提高數據交換速度)

15.When using discovery, avoid cardinality of “some”.

當使用服務發現機制的時候,避免使用不確定的基數(WCF4.0裡,查找終結點的個數,這裡明確數目,FindCriteria.MaxResults = 1)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved