2013-01-09 110 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