<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>
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 nUserID = (Int32) Session["userID"]
}
}
}
if ( -1 == nUserID )
{
throw new ApplicationException ( "Unexpected situation: userID invalid." );
}
this.doSomething( nUserID );
这样的代码会遍布各处。
那么,利用他的这个封装方案来做重构,确实让代码简洁干净了不少!
经过他的封装,上面的代码用这么一句话就行了:
this.doSomething( CCurrentSession.UserID )
他的类其实也很简单,如下所示:
usingSystem;
usingSystem.Web;
/**////--------------------------------------------------------------------
///DevelopedbyM.vanEijkel-aug2005
///[e]:marcelvaneijkel@gmail.com
///[w]:www.vaneijkel.com
namespaceVanEijkel.Web
{
/**////<summary></summary>
///Wrapperclassforthesessionobject.
///Itcentralizesthelogicforretrievingandvalidationofsessioninformation.
///Byusinganapproachlikethisyouimprovetheprotectionandencapsulationofexistingcode.
///Itoffersasimple,low-risk,easymanageablewaytoimproveexistingWebApplication.
///Therfore,IcallitwebRefactoring.
///
publicclassCurrentSession
{
Constants#regionConstants
privateconstStringsMANDATORY_SESSION_KEY_NOT_FOUND_MSG="Sessionvariableexceptedbutdoesnotexist.Key={0}";
privateconstStringsMANDATORY_SESSION_VALUE_INVALID_NULL="Nonenullsessionvalueexcepted.Key={0}";
privateconstInt32nUSERID_UNKOWN=-1;
privateconstInt32nUSERID_MINIMUM=1;
privateconstStringsUSERID_INVALID="InvalidUserID:{0}.UserIDshouldbelargerthan:{1}";
#endregion
UserID#regionUserID
/**////<summary></summary>
///ReturnstheuserIDasaInt32insteadofanobject.
///Thiswayyouwillgetthecompilerprotectionandintelligencesupportyouneed.
///
publicstaticInt32UserID
{
get
{
return(Int32)GetValueOrDefault(eKeys.UserID,nUSERID_UNKOWN);
}
set
{
if(nUSERID_MINIMUM>=value)
{
thrownewApplicationException(String.Format(sUSERID_INVALID,value,nUSERID_MINIMUM));
}
SetValue(eKeys.UserID,value);
}
}
#endregion
private:GetValueOrDefault(eKeyseKey,ObjectoDefaultValue)#regionprivate:GetValueOrDefault(eKeyseKey,ObjectoDefaultValue)
/**////<summary></summary>
///Getsthevaluefromthesessionobject.
///
///Thesessionkeytogetthevaluefor.
///Thedefaultvaluetouseifnovalidvaluestored.
///<returns></returns>Whenthevalueisnullorthekeydoesnotexist,
///thespecifieddefaultvalueisreturned.
///Otherwise,thevalueisreturned
privatestaticobjectGetValueOrDefault(eKeyseKey,ObjectoDefaultValue)
{
//getthevalue
objectoValue=GetValue(eKey);
//valuenotfoundornull?
if(null==oValue)
{
//returndefaultvalue
returnoDefaultValue;
}
//everythingoke:returnsessionvalue
returnoValue;
}
#endregion
private:GetMandatoryValue(eKeyseKey)#regionprivate:GetMandatoryValue(eKeyseKey)
/**////<summary></summary>
///Returnsthesessionvalueforasession-keythatmustexist.
///IfthekeydoesnotexistanapplicationExceptionisthrown.
///
///Thesession-keytoreturnthesession-valuefor.
///<returns></returns>Anone-nullvalue.
privatestaticobjectGetMandatoryValue(eKeyseKey)
{
//getthevalue
objectoValue=GetValue(eKey);
//keynotfoundorvaluenull?
if(null==oValue)
{
//throwapplicationExceptionbecauseitsapplicationlogicerror(noneCLR)
thrownewApplicationException(String.Format(sMANDATORY_SESSION_KEY_NOT_FOUND_MSG,eKey.ToString()));
}
//everythingoke:returnvalue
returnoValue;
}
#endregion
private:GetValue(eKeyseKey)#regionprivate:GetValue(eKeyseKey)
/**////<summary></summary>
///Getsthesessionvaluefromthespecifiedkey.
///
///Thekeytogetthevaluefrom
///<returns></returns>Thesessionvalueforthespecifiedsessionkey.
///Ifthekeydoesnotexist,nullisreturned.
///
privatestaticobjectGetValue(eKeyseKey)
{
returnHttpContext.Current.Items[eKey.ToString()];
}
#endregion
privateSetMandatoryValue(eKeyseKey,ObjectoValue)#regionprivateSetMandatoryValue(eKeyseKey,ObjectoValue)
privatestaticvoidSetMandatoryValue(eKeyseKey,ObjectoValue)
{
if(null==oValue)
{
thrownewApplicationException(String.Format(sMANDATORY_SESSION_VALUE_INVALID_NULL,eKey.ToString()));
}
}
#endregion
privateSetValue(eKeyseKey,ObjectoValue)#regionprivateSetValue(eKeyseKey,ObjectoValue)
/**////<summary></summary>
///Storesthespecifiedsession-valuetothespecifiedsession-key.
///
///Thekeyforthevaluetostoreinthesession.
///Thevaluetostoreinthesession
privatestaticvoidSetValue(eKeyseKey,ObjectoValue)
{
HttpContext.Current.Items[eKey.ToString()]=oValue;
}
#endregion
/**////<summary></summary>
///Anenumforthe
///
privateenumeKeys
{
UserID
}
}
}
分享到:
相关推荐
ASP.NET的 Session 详解
ASP.NET简洁论坛源代码免费下载ASP.NET简洁论坛源代码免费下载ASP.NET简洁论坛源代码免费下载ASP.NET简洁论坛源代码免费下载ASP.NET简洁论坛源代码免费下载ASP.NET简洁论坛源代码免费下载ASP.NET简洁论坛源代码免费...
C#本科毕业设计基于ASP.NET的人事管理系统源代码C#本科毕业设计基于ASP.NET的人事管理系统源代码C#本科毕业设计基于ASP.NET的人事管理系统源代码C#本科毕业设计基于ASP.NET的人事管理系统源代码。C#本科毕业设计基于...
asp.net 人事管理系统 源代码 asp.net 人事管理系统 源代码 asp.net 人事管理系统 源代码 asp.net 人事管理系统 源代码 asp.net 人事管理系统 源代码 asp.net 人事管理系统 源代码
ASP.NET 中 Session 实现原理浅析 如 session的说明 使用等等
在后台应用程序中设置:Session.Timeout = 1; 注意:1、此方法可以设置在一个公共的页面中,然后直接调用即可。 2、时间是以分钟为单位的 方法二: 在Web.config中设置: 在<system></system>中写入...
ASP.NET中简单的Session、Application、Cookie的小例子
ASP.NET简洁论坛源代码 ASP.NET简洁论坛源代码ASP.NET简洁论坛源代码 ASP.NET简洁论坛源代码
简单的asp.net三层架构学习代码,对初学者挺有用的
ASP.NET写的轮播图代码页面,可以参考这个写法设计自己的asp.net页面或者网站,可以用于实验或者课程报告等
ASP.NET报名系统(源代码+论文)下载.zip ASP.NET报名系统(源代码+论文)下载.zip ASP.NET报名系统(源代码+论文)下载.zip ASP.NET报名系统(源代码+论文)下载.zip ASP.NET报名系统(源代码+论文)下载.zip ASP.NET报名系统...
ASP.NET学习积累的代码段.rarASP.NET学习积累的代码段.rarASP.NET学习积累的代码段.rarASP.NET学习积累的代码段.rarASP.NET学习积累的代码段.rarASP.NET学习积累的代码段.rarASP.NET学习积累的代码段.rarASP.NET学习...
通过本书的学习,读者可以全面掌握ASP.NET MVC的开发,并从代码中获取软件开发与架构设计的经验与灵感。本书具有很大的参考价值,既适合ASP.NET MVC开发初学者阅读,也适合有一定基础的ASP.NET MVC开发人员进行技术...
使用Asp.net + C#开发的项目管理系统。
ASP.net酒店管理系统源代码ASP.net酒店管理系统源代码ASP.net酒店管理系统源代码ASP.net酒店管理系统源代码ASP.net酒店管理系统源代码ASP.net酒店管理系统源代码ASP.net酒店管理系统源代码ASP.net酒店管理系统源代码...
asp.net 新闻发布系统源代码...asp.net 新闻发布系统源代码...asp.net 新闻发布系统源代码...asp.net 新闻发布系统源代码
ASP.NET电子商务网站源代码ASP.NET电子商务网站源代码。。
asp.net 网络开发指南源代码