2010-03-08 26 views
1

如何創建從數據庫讀取設置的應用程序範圍實用程序/管理器類?我應該使用靜態類,例如在asp.net中創建和存儲實用程序/管理器類的正確方法mvc

public static class ThemeHelper 
{ 
    private static string themeDirectory { get; private set; } 

    static ThemeHelper() 
    { 
     // read from the database 
     themeDirectory = "blue-theme"; 
    } 

    public static string ResolveViewPath(string viewName) 
    { 
      string path = string.Format("~/themes/{0}/{1}.aspx", themeDirectory, viewName); 
      // check if file exists... 
      // if not, use default 
      return path; 
    } 
} 

或者一個普通類的靜態實例,它存儲在HttpApplicationState中,例如? 還是應該使用依賴注入庫(如Ninject)?

回答

0

對於這種場景,靜態類和方法都可以。但請注意靜態變量 - 它們的值將在同一Web應用程序的所有客戶端之間共享。

如果您希望緩存結果但區分客戶端(會話),則可以將其放到Session集合中。

相關問題