首页 > 开发 > 综合 > 正文

重构Session确实让代码简洁干净了不少

2024-07-21 02:29:14
字体:
来源:转载
供稿:网友
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;
  
  /**////--------------------------------------------------------------------
  /// developed by m. van eijkel - aug 2005
  /// [e]: [email protected]
  /// [w]: www.vaneijkel.com
  
  namespace vaneijkel.web
  {
   /**//// <summary>
   /// wrapper class for the session object.
   /// it centralizes the logic for retrieving and validation of session information.
   /// by using an approach like this you improve the protection and encapsulation of existing code.
   /// it offers a simple, low-risk, easy manageable way to improve existing webapplication.
   /// therfore, i call it webrefactoring.
   /// </summary>
   public class currentsession
   {
   constants#region constants
   private const string smandatory_session_key_not_found_msg = "session variable excepted but does not exist. key={0}";
   private const string smandatory_session_value_invalid_null = "none null session value excepted. key={0}";
  
   private const int32 nuserid_unkown = -1;
   private const int32 nuserid_minimum = 1;
   private const string suserid_invalid = "invalid userid:{0}. userid should be larger than:{1}";
   #endregion
  
   userid#region userid
   /**//// <summary>
   /// returns the userid as a int32 instead of an object.
   /// this way you will get the compiler protection and intelligence support you need.
   /// </summary>
   public static int32 userid
   {
   get
   {
   return (int32) getvalueordefault( ekeys.userid, nuserid_unkown );
   }
   set
   {
   if ( nuserid_minimum >= value )
   {
   throw new applicationexception ( string.format(suserid_invalid, value, nuserid_minimum ));
   }
   setvalue( ekeys.userid, value );
   }
   }
   #endregion
  
   private: getvalueordefault( ekeys ekey, object odefaultvalue )#region private: getvalueordefault( ekeys ekey, object odefaultvalue )
   /**//// <summary>
   /// gets the value from the session object.
   /// </summary>
   /// <param name="ekey"> the session key to get the value for.</param>
   /// <param name="odefaultvalue">the default value to use if no valid value stored.</param>
   /// <returns>when the value is null or the key does not exist,
   /// the specified default value is returned.
   /// otherwise, the value is returned</returns>
   private static object getvalueordefault( ekeys ekey, object odefaultvalue )
   {
   //get the value
   object ovalue = getvalue( ekey );
  
   //value not found or null?
   if (null == ovalue)
   {
   //return default value
   return odefaultvalue;
   }
  
   //everything oke: return session value
   return ovalue;
   }
   #endregion
   private: getmandatoryvalue( ekeys ekey )#region private: getmandatoryvalue( ekeys ekey )
   /**//// <summary>
   /// returns the session value for a session-key that must exist.
   /// if the key does not exist an applicationexception is thrown.
   /// </summary>
   /// <param name="ekey"> the session-key to return the session-value for. </param>
   /// <returns> a none-null value.</returns>
   private static object getmandatoryvalue( ekeys ekey )
   {
   //get the value
   object ovalue = getvalue( ekey );
  
   //key not found or value null?
   if ( null == ovalue )
   {
   //throw applicationexception because its application logic error (none clr)
   throw new applicationexception ( string.format( smandatory_session_key_not_found_msg, ekey.tostring() ));
   }
  
   //everything oke: return value
   return ovalue;
   }
   #endregion
   private: getvalue( ekeys ekey )#region private: getvalue( ekeys ekey )
   /**//// <summary>
   /// gets the session value from the specified key.
   /// </summary>
   /// <param name="ekey">the key to get the value from</param>
   /// <returns>the session value for the specified session key.
   /// if the key does not exist, null is returned.
   /// </returns>
   private static object getvalue( ekeys ekey )
   {
   return httpcontext.current.items[ ekey.tostring() ];
   }
   #endregion
  
   private setmandatoryvalue( ekeys ekey, object ovalue )#region private setmandatoryvalue( ekeys ekey, object ovalue )
   private static void setmandatoryvalue( ekeys ekey, object ovalue )
   {
   if ( null == ovalue )
   {
   throw new applicationexception( string.format(smandatory_session_value_invalid_null, ekey.tostring() ) );
   }
   }
   #endregion
   private setvalue( ekeys ekey, object ovalue)#region private setvalue( ekeys ekey, object ovalue)
   /**//// <summary>
   /// stores the specified session-value to the specified session-key.
   /// </summary>
   /// <param name="ekey">the key for the value to store in the session.</param>
   /// <param name="ovalue">the value to store in the session</param>
   private static void setvalue ( ekeys ekey, object ovalue)
   {
   httpcontext.current.items[ekey.tostring()] = ovalue;
   }
   #endregion
  
   /**//// <summary>
   /// an enum for the
   /// </summary>
   private enum ekeys
   {
   userid
   }
   }
  }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表