繁体   English   中英

在ASP.NET MVC应用程序的实例之间共享具有非静态成员的静态类?

[英]Static class with non-static members shared across instances of ASP.NET MVC app?

我有几个项目的解决方案,包括ASP.NET MVC项目和WPF应用程序。 在数据库中,我有一些常规设置要在两个应用程序中使用。 为此,我创建了一个类库Foo ,该类库将设置加载到字典中,并提供Get(string key)方法以从字典中访问特定设置。 由于设置可以被用户覆盖,因此我添加了一个包含UserId的属性。 Get()方法自动负责检查和使用UserId属性。 这样,我不需要每次调用Get()方法时都将UserId作为参数传递。

对于WPF应用程序,这工作得很好,因为只有一个实例正在运行。 但是对于Web项目,我只希望字典填充一次(在Application_Start() ),并且所有访问该站点的用户都可以使用该字典。 如果我将类实例设为静态,则效果很好。 但是,这不允许我拥有不同的UserIds ,因为访问该网站的每个用户的每个用户都将被覆盖。 解决此问题的最佳方法是什么?

这是我到目前为止尝试过的(非常简化):

类库:

public class Foo ()
{
    private Dictionary<string, string> Res;
    private int UserId;

    public Foo ()
    {
        Res = DoSomeMagicAndGetMyDbValues();
    }

    public void SetUser (int userId)
    {
        UserId = userId;
    }

    public string Get(string key)
    {
        var res = Res[key];

        // do some magic stuff with the UserId

        return res;
    }
}

Global.asax:

public static Foo MyFoo;

protected void Application_Start()
{
    MyFoo = new Foo();
}

UserController.cs:

public ActionResult Login(int userId)
{
    MvcApplication.MyFoo.SetUser(userId); // <-- this sets the same UserId for all instances
}

如何将设置存储在Dictionary<int<Dictionary<string, string>> ,其中外层词典的KeyUserId ,而键0保存为默认设置? 当然,这意味着您必须将用户ID传递给Get和Set方法。

然后,您可以执行以下操作:

public static class Foo
{
    private static Dictionary<int, Dictionary<string, string>> settings;

    /// <summary>
    /// Populates settings[0] with the default settings for the application
    /// </summary>
    public static void LoadDefaultSettings()
    {
        if (!settings.ContainsKey(0))
        {
            settings.Add(0, new Dictionary<string, string>());
        }

        // Some magic that loads the default settings into settings[0]
        settings[0] = GetDefaultSettings();
    }

    /// <summary>
    /// Adds a user-defined key or overrides a default key value with a User-specified value
    /// </summary>
    /// <param name="key">The key to add or override</param>
    /// <param name="value">The key's value</param>
    public static void Set(string key, string value, int userId)
    {
        if (!settings.ContainsKey(userId))
        {
            settings.Add(userId, new Dictionary<string, string>());
        }

        settings[userId][key] = value;
    }

    /// <summary>
    /// Gets the User-defined value for the specified key if it exists, 
    /// otherwise the default value is returned.
    /// </summary>
    /// <param name="key">The key to search for</param>
    /// <returns>The value of specified key, or empty string if it doens't exist</returns>
    public static string Get(string key, int userId)
    {
        if (settings.ContainsKey(userId) && settings[userId].ContainsKey(key))
        {
            return settings[userId][key];
        }

        return settings[0].ContainsKey(key) ? settings[0][key] : string.Empty;
    }        
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM