ASP.NET MVC如何避免静态变量? [英] ASP.NET MVC How to avoid static variables?

查看:555
本文介绍了ASP.NET MVC如何避免静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近发布了关于质疑不安全的静态变量是如何,我发现因为我需要摆脱他们。但我无法弄清楚如何?正想着,一个静态的get()为每类方法,它返回一个实例,但该实例必须声明为static。

因此​​,要做到这一点的唯一方法,是有实例的引用(每个辅助,即用户helper.cs,imagehelper.cs等),以它们声明为实例属性上的某种全局访问类的?但是哪个班?有什么事,我在这里丢失?

code以下示例类的,我需要改变:

 唱系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用Mvc.Mailer;命名空间MVCWebsite.Helpers
{
        公共类的AppSettings
        {
                公共静态无效OnAppInit()
                {
                        //一般
                        AppName的=MyApp的;
                        DesktopBaseURLs =新词典<字符串,字符串>();
                        DesktopBaseURLs.Add(开发,本地主机:50560);
                        DesktopBaseURLs.Add(测试,www.test.whatever.com);
                        DesktopBaseURLs.Add(活,www.whatever.com);
                        MobileBaseURLs =新词典<字符串,字符串>();
                        MobileBaseURLs.Add(dev的,m.local.whatever.com);
                        MobileBaseURLs.Add(测试,m.test.whatever.com);
                        MobileBaseURLs.Add(活,m.whatever.com);                        //电子邮件
                        EmailHostName = AppName的+的.com; //眼下ATLEAST
                        NoReplyEmailAddress =无回复@+ EmailHostName.ToLower();
                        SupportEmailAddress =@支持+ EmailHostName.ToLower();
                        ErrorEmailAddress =错误@+ EmailHostName.ToLower();                        //资源
                        TempFileURL =/内容/温度/;
                        UserDataURL =/内容/用户内容/;
                        ProfilePicturesURL = UserDataURL +的个人资料,照片/;                        VAR一个= GlobalHelper.GetURLAsServerPath(ProfilePicturesURL);
                        变种B = A;                }                //一般
                公共静态字符串AppName的{搞定;组; }
                公共静态字典<字符串,字符串> DesktopBaseURLs;
                公共静态字典<字符串,字符串> MobileBaseURLs;                //电子邮件
                公共静态字符串EmailHostName {搞定;组; }
                公共静态字符串NoReplyEmailAddress {搞定;组; }
                公共静态字符串SupportEmailAddress {搞定;组; }
                公共静态字符串ErrorEmailAddress {搞定;组; }                //资源
                公共静态字符串UserDataURL {搞定;组; }
                公共静态字符串TempFileURL {搞定;组; }
                公共静态字符串ProfilePicturesURL {搞定;组; }                //方法
                公共静态无效SetAppURL()
                {                }
        }
}


解决方案

我建议您为您的AppSettings类的接口,这样就可以在你的控制器现在用它,实现它以不同的方式您认为合适的:

 公共接口IAppSettings
{
    字符串AppName的{搞定;组; }
    ...
}

您可以然后立即与你的静态类通过包装类实现它:

 公共类AppSettingsWrapper:IAppSettings
{
    公共AppName的
    {
        得到
        {
            返回AppSettings.AppName;
        }
        组
        {
            AppSettings.AppName =价值;
        }
    }    ...
}

之后,你可以创建一个使用会话,或饼干,或数据库值,或任何IAppSettings的实现。最重要的是抽象的,你存储的东西,这样就可以在满足您需要的方式实行的方式。

recently posted about questioning how unsafe static variables are, I've since discovered I need to get rid of them. But I cannot figure out how to? Was thinking a static Get() method for each class, that returns a single instance, but then that instance would have to be declared static.

So the only way to do it, is to have the instance references (for each helper, I.E user helper.cs, imagehelper.cs etc.) is to declare them as instance properties on some sort of globally accessible class? But which class? Is there something I'm missing here?

Code below of a sample class I need to change:

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()
                {

                }
        }
}

解决方案

I recommend creating an interface for your AppSettings class, so that you can use it in your controllers now, and implement it in different ways as you see fit:

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

You can then implement it immediately with your static class via a wrapper class:

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

    ...
}

Later on, you can create an implementation of IAppSettings that uses session, or cookies, or database values, or whatever. The important thing is to abstract the way you store things so that you can implement in a way that meets your needs.

这篇关于ASP.NET MVC如何避免静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆