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

ASP.NET
漫谈.Net开发关于命名空间和目录划分
.net中关于企业Excel报表的生成
.NET中取得IP/用户名等信息常用方法
关于.Net开发下的分布式缓存设计
.NET中为组合框添加自动查询功能
ASP.NET如何进行性能优化问题
开发中如何有效监控.NET应用程序
ASP.NET2.0 显示写入日期和时间语法
用.NET Array类的Sort方法分类数值
Asp.net Mvc Pv4中使用AjaxHelper
Asp.net中Forms验证的角色验证授权(一)
Asp.net中Forms验证的角色验证授权(二)
Asp.Net2.0数据库基本操作方法学习
Asp.Net之枚举类型输出需要类型转换
用.NET的File控件上传文件的解决方案
.NET泛型技巧之类型参数之间的转换
在ASP.NET中将数据直接输出成Excel格式
在.NET框架下使用自定义配置设置
跟ASP.NET MVC一起使用jQuery
Visual Basic中文本框处理技巧集萃

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 57 ::
收藏到网摘: 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
}
}
}