【发布时间】:2023-03-08 12:37:01
【问题描述】:
我有一个包含多个项目的解决方案,包括一个 ASP.NET MVC 项目和一个 WPF 应用程序。在数据库中,我有一些我想在两个应用程序中使用的常规设置。为此,我创建了一个类库Foo,它将设置加载到字典中,并提供Get(string key) 方法来访问字典外的特定设置。
由于用户可以覆盖这些设置,因此我添加了一个包含 UserId 的属性。 Get() 方法自动负责检查和使用UserId 属性。这样,我就不需要每次调用Get() 方法时都将 UserId 作为参数传递。
对于 WPF 应用程序,这工作得很好,因为只有一个实例正在运行。但是对于网络项目,我希望字典只填写一次(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;
}
}
全球.asax:
public static Foo MyFoo;
protected void Application_Start()
{
MyFoo = new Foo();
}
用户控制器.cs:
public ActionResult Login(int userId)
{
MvcApplication.MyFoo.SetUser(userId); // <-- this sets the same UserId for all instances
}
【问题讨论】:
-
请注意,model-view-controller 标签是针对有关模式的问题。 ASP.NET-MVC 实现有一个特定的标记。
标签: c# asp.net-mvc static