最近、静的変数がどれほど安全でないかについての質問について投稿しましたが、それ以来、それらを取り除く必要があることに気づきました。しかし、私はその方法を理解できませんか?単一のインスタンスを返す各クラスの静的なGet()メソッドを考えていましたが、その場合、そのインスタンスは静的に宣言する必要があります。
したがって、それを行う唯一の方法は、インスタンス参照(各ヘルパー、IEユーザー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 = "no-reply@" + EmailHostName.ToLower();
SupportEmailAddress = "support@" + EmailHostName.ToLower();
ErrorEmailAddress = "errors@" + 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()
{
}
}
}