[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
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
相关文章推荐
- [ASP.NET]重构Session确实让代码简洁干净了不少
- [ASP.NET]重构Session确实让代码简洁干净了不少
- [ASP.NET]重构Session确实让代码简洁干净了不少
- [ASP.NET]重构Session确实让代码简洁干净了不少
- 重构Session确实让代码简洁干净了不少
- asp.net session的使用与过期实例代码
- 免费Visual Studio 2005开发环境ASP.NET代码重构插件——Refactor! for ASP.NET
- asp.net session的使用与过期实例代码
- AOP实践--ASP.NET MVC 5使用Filter过滤Action参数防止sql注入,让你代码安全简洁
- asp.net HttpHandler操作Session的函数代码
- Refactor!™ for ASP.NET--ASP.NET代码重构插件
- 重构后的ASP.NET Time Tracker Starter Kit代码,供大家参考
- ASP.NET 例程完全代码版(6)——网站统计模块综述(Session,Application的讨论)
- ASP.NET网站管理系统退出 清除浏览器缓存,Session的代码
- 代码重构(c#&asp.net版)文摘
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(十一) 代码重构使用反射工厂解耦
- 使用依赖关系注入在 ASP.NET Core 中编写干净代码
- Asp.net中安全退出时清空Session或Cookie的实例代码
- ASP.NET - 使用依赖关系注入在 ASP.NET Core 中编写干净代码
- Asp.net中安全退出时清空Session或Cookie的实例代码