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

编程入门 行业动态 更新时间:2024-10-28 10:34:52
具有非静态成员的静态类在ASP.NET MVC应用程序的实例之间共享?(Static class with non-static members shared across instances of ASP.NET MVC app?)

我有几个项目的解决方案,包括ASP.NET MVC项目和WPF应用程序。 在DB中,我有一些常规设置,我想在两个应用程序中使用。 为此,我创建了一个类库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
}

I have a solution with several projects, including an ASP.NET MVC project and a WPF application. In the DB, I have some general settings which I want to use in both applications. To do that, I've created a class library Foo which loads the settings into a dictionary and provides a Get(string key) method for accessing specific settings out of the dictionary. Since the settings can be overridden by user, I've added a property containing the UserId. The Get() method automatically takes care of checking and using the UserId property. This way, I don't need to pass the UserId as a param each time I call the Get() method.

For the WPF application, this works just fine, since there is just one instance running. However for the web project, I'd like to have the dictionary filled only once (in Application_Start()) and be accessible to all users visiting the site. This works fine if I make the class instance static. However, that does not allow me to have different UserIds, as this would be overridden for everyone with every user that accesses the site. What's the best way to solve this?

Here's what I tried so far (very simplified):

Class Library:

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>> ,其中外部字典的Key是UserId ,其中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; } }

What about storing the settings in a Dictionary<int<Dictionary<string, string>>, where the Key of the outer dictionary is the UserId, with key 0 saved for the default settings? Of course this means you'd have to pass the user id to the Get and Set methods...

Then, you could possibly do something like this:

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; } }

更多推荐

UserId,Get,Foo,电脑培训,计算机培训,IT培训"/> <meta name="description&qu

本文发布于:2023-07-14 17:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   应用程序   实例   成员   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!