初学编程的朋友往往喜欢收集一些很“奇妙”的编程技巧,然而,技巧的积累往往并没有提高程序质量,反而引导一些编程者一味追求奇和新,忘记了基本编程习惯的培养,不利于团队的合作,可能,这也是中国并不缺少聪明的程序员,但是缺少聪明的开发团队的一个原因吧。在asp.net的开发中,可以学习的技巧不少,但是,一些基本的编程习惯我们一定要养成,这样不但能根本上提高程序质量和开发效率,而且,也利于程序的阅读和团队开发。如果自己写的程序只有自己可以看懂或者只有几个人可以看懂,即使程序技巧神乎其技,对于程序的升级和维护都是致命问题。
一、 错误(以外)的处理
程序健壮性最基本要求就是程序错误的处理与捕捉,在asp.net中,错误的处理有和其他编程语言一样的机制,可以使用try…catch…finally等方式,这一点和asp相比具有较大的进步。而且,使用这些错误处理方法,可以大大提高程序的可读性和程序调试速度,在这几个优势结合的情况下,我们更加应该注意这一点。
关于错误的处理,我们可以参考这篇文章(英文):
http://www.123aspx.com/redir.aspx?res=28336
二、 字符串的处理
网页设计中,字符串的处理几乎是最常见的了。使用asp.net以后,字符串的处理比asp的速度快,而且,在asp.net中,专门增加一个字符串处理类stringbulider,使用这个类可以完成一些常见的字符串操作,而最主要的,使用stringbuilder可以大大提高字符串处理速度。
在asp.net中,最常见的就是使用“&”来连接两个字符串:
dim myoutputstring as string = "my name is"
dim myinputstring as string = " alex"
myoutputstring = myoutputstring & myinputstring
response.write(myoutputstring)
现在,我们来看看stringbuilder的使用,在使用stringbuilder的时候,我们对字符串可以做一些基本的操作,比如append、replace、insert、remove等,现在我们来看具体举例。
(1)stringbuilder中append的使用
append和其他语言的append一样,就是在字符串最后增加其他字符。
dim sb as stringbuilder = new stringbuilder()
sb.append( "<table border=1 width=80%>" )
for i = 0 to rowcount – 1
sb.append("<tr>")
for k = 0 to colcount – 1
sb.append("<td>")
sb.append( dt.rows(i).item(k, datarowversion.current).tostring())
sb.append( "</td>" )
next
sb.append("<tr>")
next
sb.append( "</table>")
dim stroutput as string = sb.tostring()
lblcompany.text = stroutput
在以上的程序中,用append方法实现了一个表格的输出,需要注意的一点是,stringbulider必须首先使用tostring()方法将其转化为string类型才可以直接输出。在以上的举例中,我们看到的全部是append一个直接的字符串,其实,这个方法有一个很方便的功能,那就是可以直接append其他类型的变量,比如可以直接appemd一个integer类型的数值,当然,我们输出以后自动转化为一个字符串:
sub page_load(source as object, e as eventargs)
dim sb as system.text.stringbuilder
dim varother as integer
varother=9999
sb =new system.text.stringbuilder()
sb.append("<font color=blue>可以append其他类型:</font>")
sb.append(varother)
response.write(sb.tostring())
end sub
(2)字符串中其他方法的使用
我们还可以使用其他方法,我们来看看常见的:
insert方法,可以在指定位置插入其他字符,使用方法:insert(插入位置,插入字符);
remove方法,可以在指定位置删除指定字数字符,使用方法:remove(其实位置,字符数);
replace方法,可以替换指定字符,使用方法:replace(被替换字符串,替换字符串)
字符串的具体介绍和使用方法可以参考以下文章(英文):
http://aspfree.com/aspnet/stringbuilder.aspx
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemtextstringbuilderclasstopic.asp
三、 数据库链接connection和datareader的关闭
在使用asp编程的时候,我们就已经知道,在使用数据库连接以后,一定要将连接关闭,然后设置为nothing。在asp.net中,我们仍然需要这样使用,不过,在asp.net中,由于使用了ado.net,所以,在一些相关的处理方面,实际还是有一些细微的区别,而这些区别,往往也就是我们设计的时候最需要注意的。现在,我们通过举例,来看看在常见的ado.net操作中,需要注意哪些问题。
(1)举例一
dim myconnection as sqlconnection = new sqlconnection(configurationsettings.appsettings("dsn_pubs"))
dim mycommand as sqlcommand = new sqlcommand("select pub_id, pub_name from publishers", myconnection)
dim mydatareader as sqldatareader
try
myconnection.open()
mydatareader = mycommand.executereader(commandbehavior.closeconnection)
dropdownlist1.datasource = mydatareader
dropdownlist1.databind()
catch myexception as exception
response.write("an error has occurred: " & myexception.tostring())
finally
if not mydatareader is nothing then
关闭datareader
mydatareader.close()
end if
end try
在以上的举例中,我们注意到,这里只关闭了datareader,并没有关闭connection。为什么呢?仔细观察以上的executereader方法,原来,设置了executereader参数,当执行完executereader以后,会自动关闭connection。所以,这样设置以后,就没有必要再手动关闭connection了。
(2)举例二
dim myconnection as sqlconnection = new sqlconnection(configurationsettings.appsettings("dsn_pubs"))
dim mycommand as sqlcommand = new sqlcommand("select pub_id, pub_name from publishers", myconnection)
try
myconnection.open()
dropdownlist1.datasource = mycommand.executereader()
dropdownlist1.databind()
catch myexception as exception
response.write("an error has occurred: " & myexception.tostring())
finally
if not myconnection is nothing andalso ((myconnection.state and connectionstate.open) = connectionstate.open) then
myconnection.close()
end if
end try
在以上的举例中,我们发现,居然没有关闭datareader。为什么呢?其实上面的代码中,没有直接生成datareader对象,当然也就无从关闭了。需要注意一点的是,在关闭connection之前,程序首先判断connection是否已经打开,如果没有打开,也就没必要关闭了。
四、使用web.config/maching.config保存常用数据
一些数据我们需要时常使用,比如使用ado.net的时候,最常见的就是数据库连接语句,在asp中,我们常常将这些信息保存在application中。当然,在asp.net中,也可以这样,不过,asp.net已经提供一个配置文件web.config,所以,我们最好将这些信息保存在web.config中,当然,我们也可以保存在machine.config中,不过,这样的话,整个网站都必须使用,所以,一般我们都使用web.config。现在,我们来看具体这个文件的使用。
(1)web.config文件的设置
首先,我们来看web.config的设置,我们在这个文件中增加设置以下两个项目,设置如下:
<configuration>
<appsettings>
<add key="dsn" value="myserver"/>
<add key="someotherkey" value="somevalue"/>
</appsettings>
</configuration>
(2)变量的使用
以上xml文件设置了dsn和someotherkey两个变量,现在我们看看程序中怎样使用:
<html>
<script language="vb" runat=server>
sub page_load(sender as object, e as eventargs)
dim appsettings as hashtable = context.getconfig("appsettings")
dsn.text = appsettings("dsn")
someother.text = appsettings("someotherkey")
end sub
</script>
<body>
dsn setting: <asp:label id="dsn" runat=server/> <br>
some other setting: <asp:label id="someother" runat=server/>
</body>
</html>
上面的程序我们看到,使用这样定义的变量很简单也很方便。
五、使用.net的方式调试程序
asp程序的调试一直是编写asp最难的地方,这一点,asp程序员大概都深有体会,因为大家都是使用response.write来调试。而这样调试最大的缺点是,当我们调试完毕,必须一个个来删除或者注释掉这些信息,想一想,如果程序代码达到几百行或者页面很多的程序,这样的工作是多么枯燥,最怕一点,忘记将这些调试用的write删除,可能在用户使用的时候就会出现一些不雅的调试信息。
使用asp.net以后,我们可以直接定义trace来实现程序的调试。以上提到的麻烦可以轻松解决,熟悉,trace可以通过具体页面和在web.config配置文件中来定义实现,这样,当程序调试完毕以后,直接将trace设置为off就可以了,这样,程序就不会有调试功能了。
(1)页面调试的实现
在一个具体的页面需要实现调试功能的时候,我们可以这样设置:
<%@ page language="vb" trace="true" %>
(2)定义web.config实现
在web.config中,我们也可以实现程序调试的打开:
<configuration>
<system.web>
<trace enabled="true" requestlimit="10" localonly="false"/>
</system.web>
</configuration>
使用以上的设置打开trace以后,我们在具体的程序中就可以使用trace来调试程序了,比如:
trace.write("this is some custom debugging information")
或者调试程序变量:
trace.write("this is is my variable and its value is:" & myvariable.tostring())
以上设置我们可以看出,在asp.net中,程序调试功能已经很方便简单了,我们在程序设计中如果忽略这些特点,继续采用asp的思维来设计程序,那么我们的程序不但效率没有提高,也增加了其他开发者合作的难度。
六、总结
以上的一些程序编写习惯,我们可以慢慢养成,在程序设计的时候,不要太在意程序是否最简洁灵活,对于一般开发者而言,程序规范化和可读性可能比追求程序的灵活性更加重要。在互联网资源越来越丰富的情况下,我们可以参考一些很规范的程序源代码来学习,当然,最好的莫过于微软自己的东西,我们可以参考以下网址:http://www.asp.net,关于更多的程序编写问题,我们可以参考以下网址:
http://www.gotdotnet.com/team/asp/asp.net%20performance%20tips%20and%20tricks.aspx
