2008-12-03 110 views
0

我正在構建一個Web應用程序,該應用程序使用外部構建的類來處理網站的大部分工作和規則。大多數頁面都需要訪問此類來獲取它需要顯示的信息。在過去,我會將這樣的類放入會話變量中,因此在需要時可以輕鬆訪問,而不需要不斷重新實例化。維護網站類的最佳實踐

第一個問題,將這個類填充到會話變量(這不是很大)這是一個壞主意?

第二個問題,如果在會話中存儲站點應用程序層類並不是一個好主意,那麼有沒有一種方法可以編寫一個集中式方法來使用該類來抓取或存儲類到會話中?我不想在頁面獲得課程後使用一堆重複的代碼頁,檢查它是否存在,創建它是否存在,等等。

回答

0

不知道您使用的任何框架,似乎並不聰明班級存儲到會話存儲中 - 每個用戶將被複制一次 - 除非它具有對該用戶唯一的數據。

我可以給出的最好的建議是有一個singleton類(你可以google它 - 它是一個設計模式),然後只在該類中有一個函數返回你需要的類,或者創建它if它還不存在。

3

決定在哪裏存儲類之前,你必須回答兩個問題:

  1. 多久這個類應該活?
  2. 應該在什麼範圍內可見?

回答兩個問題的示例:請求,用戶會話,應用程序。

如果這個類是無狀態的(沒有數據和唯一的邏輯),那麼它可能在應用程序的整個生命期間都可以生存。如果這只是每個用戶唯一的數據(不應在每個請求中重新加載),那麼您可以將其直接放入會話並跳過以下段落。

現在,在你決定生命長度後,你有幾個解決方案。生活方式管理的最佳解決方案是IoC容器。更簡單的解決方案是抽象存儲並使用靜態外觀,如Current.MyClass,其中MyClass實例存儲在請求,會話或應用程序中,具體取決於向Current提供的存儲。

但是,您不應該在指定的類中實現singleton,因爲它本身不應該自己決定需要多少個實例,並且它會限制您在需要時使用具有相同接口的另一個類進行替換的能力。

0

如果班級應該首先存儲在會話中,陪審團仍然沒有。有了這個應用程序,我問了這個問題,我選擇了不把課程填入會話中,這真的沒有必要,我很懶。使用這種方法來管理會話總是值得的,因爲它在web開發中困擾了我一段時間。

我的研究結果是編寫一個靜態類來管理我的會話變量。這個類處理對會話的所有讀寫操作,並保持它們全部強類型以便引導。它一直困擾着我使用重複的代碼遍佈全球的會話廢話。也保持拼寫錯誤的方式。

有兩篇我喜歡的文章,我現在只能找到其中的一篇文章,當我找到它時會包含其他文章。

第一次是在Code Project 這可以是第二link

的圖案是簡單和直接的。我還爲從url查詢字符串獲取參數的請求構建了一個類。我沒有理由不把它擴展到cookies。

這是我第一次使用模式,我只使用字符串,所以私有方法有點有限,但可以很容易地將其更改爲使用任何類或基本類型。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Configuration; 

namespace BDZipper.Site 
{ 
    /// <summary> 
    /// This class maintains all session variables for us instead of handeling them 
    /// individually in the session. They are also strongly typed. 
    /// </summary> 
    public static class SessionManager 
    { 

     # region Private Constants 
     // Define string constant for each property. We use the constant to call the session variable 
     // easier not to make mistakes this way. 
     // I think for simplicity, we will use the same key string in the web.config AppSettings as 
     // we do for the session variable. This way we can use the same constant for both! 
     private const string startDirectory = "StartDirectory"; 
     private const string currentDirectory = "CurrentDirectory"; 

     # endregion 

     /// <summary> 
     /// The starting directory for the application 
     /// </summary> 
     public static string StartDirectory 
     { 
      get 
      { 
       return GetSessionValue(startDirectory, true); 
      } 
      //set 
      //{ 
      // HttpContext.Current.Session[startDirectory] = value; 
      //} 
     } 

     public static string CurrentDirectory 
     { 
      get 
      { 
       return GetSessionValue(currentDirectory, false); 
      } 
      set 
      { 
       HttpContext.Current.Session[currentDirectory] = value; 
      } 
     } 
     //TODO: Update to use any class or type 
     /// <summary> 
     /// Handles routine of getting values out of session and or AppSettings 
     /// </summary> 
     /// <param name="SessionVar"></param> 
     /// <param name="IsAppSetting"></param> 
     /// <returns></returns> 
     private static string GetSessionValue(string SessionVar, bool IsAppSetting) 
     { 
      if (null != HttpContext.Current.Session[SessionVar]) 
       return (string)HttpContext.Current.Session[SessionVar]; 
      else if (IsAppSetting) // Session null with appSetting value 
       return ConfigurationManager.AppSettings[SessionVar]; 
      else 
       return ""; 
     } 
    } 
}