您的位置:首页 > 编程语言 > ASP

[ASP.NET]重构Session确实让代码简洁干净了不少

2005-08-31 19:32 405 查看
CodeProject的这篇文章确实对我有所启迪,
http://www.codeproject.com/useritems/SessionWrapper.asp#xx1208856xx

诚如作者所说,我们经常在ASP.NET用许多类似于下面的代码来检测Session中存储的对象,来防止Session过期后存储的变量丢失问题:
Int32 nUserID = -1;
if ( null != Session["userID"] ) {
if ( Session["userID"] is Int32 ) {
if ( 0 < Session["userID"] ) {
nUserID = (Int32) Session["userID"]
}
}
}
if ( -1 == nUserID )
{
throw new ApplicationException ( "Unexpected situation: userID invalid." );
}

this.doSomething( nUserID );
这样的代码会遍布各处。

那么,利用他的这个封装方案来做重构,确实让代码简洁干净了不少!
经过他的封装,上面的代码用这么一句话就行了:

this.doSomething( CCurrentSession.UserID )

他的类其实也很简单,如下所示:

using System;
using System.Web;

namespace VanEijkel.Web
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: