IDesign C#编码规范(之六)

80酷酷网    80kuku.com

  编码|规范大家中秋好,首先非常感谢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://...”;
}
}


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: