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

首頁 > 編程 > .NET > 正文

[ASP.NET]重構Session確實讓代碼簡潔干凈了不少

2024-07-10 13:06:10
字體:
來源:轉載
供稿:網友

誠如作者所說,我們經常在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
    }
    }
}


 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳江市| 丰台区| 平果县| 安远县| 太白县| 贵州省| 罗江县| 唐山市| 得荣县| 江油市| 嘉善县| 霸州市| 六安市| 凤山市| 子洲县| 鄂托克前旗| 尤溪县| 安化县| 察雅县| 镇原县| 康乐县| 项城市| 鞍山市| 区。| 麻栗坡县| 子洲县| 体育| 黑龙江省| 奈曼旗| 长乐市| 罗城| 句容市| 贵南县| 安图县| 晋江市| 建阳市| 北京市| 桑日县| 兴化市| 神木县| 衡山县|