国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

重構Session確實讓代碼簡潔干凈了不少

2024-07-21 02:29:14
字體:
來源:轉載
供稿:網(wǎng)友
codeproject的這篇文章確實對我有所啟迪,
  http://www.codeproject.com/useritems/sessionwrapper.asp#xx1208856xx。
  
  誠如作者所說,我們經(jīng)常在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 );
  這樣的代碼會遍布各處。
  
  那么,利用他的這個封裝方案來做重構,確實讓代碼簡潔干凈了不少!
  經(jīng)過他的封裝,上面的代碼用這么一句話就行了:
  
  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
   }
   }
  }
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 嘉峪关市| 临猗县| 新疆| 沙河市| 镇江市| 工布江达县| 屏东市| 剑川县| 泽普县| 镶黄旗| 南安市| 信丰县| 开化县| 吉安县| 建湖县| 连云港市| 荃湾区| 揭阳市| 米易县| 桐梓县| 咸宁市| 五台县| 莲花县| 东方市| 丹凤县| 宜都市| 漳州市| 潞西市| 海口市| 沙坪坝区| 信丰县| 松原市| 盘山县| 道真| 屯昌县| 黄骅市| 孟津县| 灵川县| 宝鸡市| 海南省| 兴化市|