誠如作者所說,我們經常在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
    }
    }
}
新聞熱點
疑難解答
圖片精選