当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 重构Session确实让代码简洁干净了不少

ASP.NET
.net开发绑定到LINQ查询的结果实例
vc.net中配置OpenOffice的SDK应用开发
用.NET读取 Flash格式文件信息
点缩略图弹出随图片大小自动调整的页面
关于VS 2008和.NET 3.5 Beta2新特性介绍
网友分享:详细解读.NET中的代码动态编译
基于.NET Framework 微软Silverlight下载
Asp.net多层架构中的变量引用与传递
VS2008中查看.NET源码的设置方法
Asp.net中如何过滤html,js,css代码
ASP.NET 2.0中CSS失效解决方案
使用ADO.NET2.0提升数据交互性能(1)
使用ADO.NET2.0提升数据交互性能(2)
使用ADO.NET2.0提升数据交互性能(3)
使用ADO.NET2.0提升数据交互性能(4)
使用ADO.NET2.0提升数据交互性能(5)
解决CSV字段数据带有双引号的问题
VB.net实现sql数据库的备份与恢复
Asp.net FMS 开发视频网站
.Net平台下开发中文语音应用程序

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 62 ::
收藏到网摘: n/a

namespaceVanEijkel.Web
{
/**////<summary>
///Wrapperclassforthesessionobject.
///Itcentralizesthelogicforretrievingandvalidationofsessioninformation.
///Byusinganapproachlikethisyouimprovetheprotectionandencapsulationofexistingcode.
///Itoffersasimple,low-risk,easymanageablewaytoimproveexistingWebApplication.
///Therfore,IcallitwebRefactoring.
///</summary>
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>
///ReturnstheuserIDasaInt32insteadofanobject.
///Thiswayyouwillgetthecompilerprotectionandintelligencesupportyouneed.
///</summary>
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>
///Getsthevaluefromthesessionobject.
///</summary>
///<paramname="eKey">Thesessionkeytogetthevaluefor.</param>
///<paramname="oDefaultValue">Thedefaultvaluetouseifnovalidvaluestored.</param>
///<returns>Whenthevalueisnullorthekeydoesnotexist,
///thespecifieddefaultvalueisreturned.
///Otherwise,thevalueisreturned</returns>
privatestaticobjectGetValueOrDefault(eKeyseKey,ObjectoDefaultValue)
{
//getthevalue
objectoValue=GetValue(eKey);

//valuenotfoundornull?
if(null==oValue)
{
//returndefaultvalue
returnoDefaultValue;
}

//everythingoke:returnsessionvalue
returnoValue;
}
#endregion
private:GetMandatoryValue(eKeyseKey)#regionprivate:GetMandatoryValue(eKeyseKey)
/**////<summary>
///Returnsthesessionvalueforasession-keythatmustexist.
///IfthekeydoesnotexistanapplicationExceptionisthrown.
///</summary>
///<paramname="eKey">Thesession-keytoreturnthesession-valuefor.</param>
///<returns>Anone-nullvalue.</returns>
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>
///Getsthesessionvaluefromthespecifiedkey.
///</summary>
///<paramname="eKey">Thekeytogetthevaluefrom</param>
///<returns>Thesessionvalueforthespecifiedsessionkey.
///Ifthekeydoesnotexist,nullisreturned.
///</returns>
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>
///Storesthespecifiedsession-valuetothespecifiedsession-key.
///</summary>
///<paramname="eKey">Thekeyforthevaluetostoreinthesession.</param>
///<paramname="oValue">Thevaluetostoreinthesession</param>
privatestaticvoidSetValue(eKeyseKey,ObjectoValue)
{
HttpContext.Current.Items[eKey.ToString()]=oValue;
}
#endregion

/**////<summary>
///Anenumforthe
///</summary>
privateenumeKeys
{
UserID
}
}
}