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://...”;
}
}