2013-01-09 104 views
-2

最近發佈了關於如何不安全的靜態變量的問題,自從我發現我需要擺脫它們。但我無法弄清楚如何?正在爲每個類想一個靜態的Get()方法,該方法返回一個單獨的實例,但是那個實例必須被聲明爲靜態的。ASP.NET MVC如何避免靜態變量?

所以唯一的方法就是讓實例引用(對於每個助手,I.E用戶helper.cs,imagehelper.cs等)是將它們聲明爲某種全局可訪問類的實例屬性?但是,哪一類?有什麼我在這裏失蹤?以下示例類的

代碼,我需要改變:

sing System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using Mvc.Mailer; 

namespace MVCWebsite.Helpers 
{ 
     public class AppSettings 
     { 
       public static void OnAppInit() 
       { 
         //General 
         AppName = "MyApp"; 
         DesktopBaseURLs = new Dictionary<string, string>(); 
         DesktopBaseURLs.Add("dev", "localhost:50560"); 
         DesktopBaseURLs.Add("test", "www.test.whatever.com"); 
         DesktopBaseURLs.Add("live", "www.whatever.com"); 
         MobileBaseURLs = new Dictionary<string, string>(); 
         MobileBaseURLs.Add("dev", "m.local.whatever.com"); 
         MobileBaseURLs.Add("test", "m.test.whatever.com"); 
         MobileBaseURLs.Add("live", "m.whatever.com"); 

         //Emails 
         EmailHostName = AppName + ".com"; //For the moment atleast 
         NoReplyEmailAddress = "[email protected]" + EmailHostName.ToLower(); 
         SupportEmailAddress = "[email protected]" + EmailHostName.ToLower(); 
         ErrorEmailAddress = "[email protected]" + EmailHostName.ToLower(); 

         //Resources 
         TempFileURL = "/content/temp/"; 
         UserDataURL = "/content/user-content/"; 
         ProfilePicturesURL = UserDataURL + "profile-pictures/"; 

         var a = GlobalHelper.GetURLAsServerPath(ProfilePicturesURL); 
         var b = a; 

       } 

       //General 
       public static string AppName { get; set; } 
       public static Dictionary<string, string> DesktopBaseURLs; 
       public static Dictionary<string, string> MobileBaseURLs; 

       //Emails 
       public static string EmailHostName { get; set; } 
       public static string NoReplyEmailAddress { get; set; } 
       public static string SupportEmailAddress { get; set; } 
       public static string ErrorEmailAddress { get; set; } 

       //Resources 
       public static string UserDataURL { get; set; } 
       public static string TempFileURL { get; set; } 
       public static string ProfilePicturesURL { get; set; } 

       //Methods 
       public static void SetAppURL() 
       { 

       } 
     } 
} 
+1

任何你沒有將這些存儲在web.config中的理由? –

+0

你爲什麼認爲你需要靜態變量? –

+1

考慮使用IoC框架。 – SLaks

回答

1

答案給你剛纔的問題明確表示,IDictionary的是在靜態方法只有不安全的變量,因爲它不是線程安全的。你只需要以不同的方式存儲這些變量。你不需要擺脫所有的靜態變量。你只需要將IDictionary改爲線程安全的東西。

順便說一句,有人有使有關的web.config一個很好的COMENT

2

我建議爲您的AppSettings類的接口,這樣就可以在你的控制器現在使用它,並以不同的方式爲實現它你認爲合適的:

public class AppSettingsWrapper : IAppSettings 
{ 
    public AppName 
    { 
     get 
     { 
      return AppSettings.AppName; 
     } 
     set 
     { 
      AppSettings.AppName = value; 
     } 
    } 

    ... 
} 

之後,你可以創建一個IM:

public interface IAppSettings 
{ 
    string AppName { get; set; } 
    ... 
} 

然後,您可以立即與您的靜態類通過包裝類實現它IAppSettings的使用,使用會話,cookie或數據庫值,或其他。重要的是抽象出你存儲事物的方式,以便你能夠以滿足你的需求的方式實現。

-1

對我想我已經想通了,它們應該作爲實例變量存儲在Global.asax.cs中。該文件包含從System.Web.HttpApplication繼承的Application類。該主類每個請求僅限於一個實例(自身)。因此,如果你在這裏存儲任何對你的幫助者的引用,你可以通過去引用他們,MvcApplication.MyHelper.DoSomething();有人請糾正我,如果這是錯誤的,但似乎對我來說。 「任何時候,一個HTTPApplication實例只能處理一個請求,所以我們不需要考慮鎖定和解鎖任何非靜態成員,但是對於我們需要的靜態成員。」-from:http://www.codeproject.com/Articles/87316/A-walkthrough-to-Application-State#c