IDesign C#編碼規范(之六)
2024-07-21 02:18:49
供稿:網友
大家中秋好,首先非常感謝xiaxia翻譯的資料。
由于我對這個文檔也是興趣濃厚,所以就把下面的部分給翻譯了。
希望大家多多指教。
4.2asp.net and web services
1.避免將代碼放入asp.net的aspx文件中。 所有的代碼都應該放在相關代碼的partial類中。
avoid putting code in aspx files of asp.net. all code should be in the code beside partial class.
2.放在相關代碼的partial類中的代碼,應該調用其他組件,而不是用于寫直接的業務邏輯。
code in code beside partial class of asp.net should call other components rather than contain direct business logic.
3.訪問線程變量之前應該檢查其是否為null。
always check a session variable for null before accessing it.
4.在事務頁面或web頁面,要將線程存儲在sql server中。
in transactional pages or web services, always store session in sql server.
5.在asp.net中,避免將服務器控件的auto-postback屬性設置為true.
avoid setting to true the auto-postback property of server controls in asp.net
6.對于asp.net頁面,運用智能瀏覽。
turn on smart navigation for asp.net pages.
7.盡量為web服務提供接口。
strive to provide interfaces for web services.
a) see appendix a of programming .net components.
8.總是為web服務提供命名空間和服務描述。
always provide namespace and service description for web services.
9.總是為web方法提供描述。
always provide a description for web methods.
10. 當加入web服務引用時,應該給與有意義的命名。
when adding a web service reference, provide meaningful name for the location.
11. 在asp.net頁面和web服務頁面,要在局部屬性里加入線程變量。只有可以訪問線程變量的屬性和其余使用屬性的代碼,不是線程變量。
in both asp.net pages and web services, wrap a session variables in a local property.
only that property is allowed to access the session variable, and the rest of the code uses the property, not the session variable.
public class calculator: webservice
{
int memory
{
get
{
int memory = 0;
object state = session[“memory”];
if(state != null)
{
memory = (int)state;
}
}
return memory;
set
{
session[“memory”] = value;
}
}
[webmethod(enablesession = true)]
public void memoryreset()
{
memory = 0;
}
}
12. 總是修改客戶web服務包裝類使其支持cookies.
always modify client-side web service wrapper class to support cookies.
a)當你無法知道服務是否服務所用線程的狀態時。
you have no way of knowing whether the service uses session state or not.
public class calculator: soaphttpclientprotocol
{
public calculatorex()
{
cookiecontainer = new system.net.cookiecontainer();
url = “http://...”;
}
}