cracker_chan
cracker_chan

Reputation: 93

thoughts on global variable with multiple users

I'm using global variables in my web application. The web application does have a log-in and can allow multiple users. The global variables are used to store data for the items currently being processed.

Will my application be in chaos if multiple users tried to access these global variables or will it be just global in their respective workstation?

If it will create chaos, what's the best way to replace the global variables.

Upvotes: 2

Views: 3857

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 157136

Don't use static variables in your ASP.NET application! They are shared across all sessions / users since they are kept in the web server, not on clients!

You have a few options, built-in already in ASP.NET:

  • Cache: application-wide, all-sessions, for temporary objects
  • Session: per-session
  • Application: application-wide, all sessions, for long time keep objects

You can easily create a property in a class that uses one of those (of course you could do this without properties, but I think they are a better practice):

public List<string> SomeSettingPerUser
{
    get
    {
        return (List<string>)HttpContext.Current.Session["SomeSetting"];
    }
    set
    {
        HttpContext.Current.Session["SomeSetting"] = value;
    }
}

Upvotes: 2

Related Questions