Global.asax, global variables and editing with code

I have two questions:

1) I have several global variables for my site, declared in my global.asax file . They are simple, small key / value pairs, and there are only a few of them. Is this good practice for small values ​​and need to be referenced on almost every page of my website? Storing them in the database and having to look up the db seems to be such that they are thought to be wasting resources on values ​​that don't change quickly.

2) If one of the values ​​changes once a week, can the user be allowed to edit the global variable using a form or some other means?

Example:

<script runat="server">

  Overloads Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

      Application("default_scoring_id") = 3
      Application("week_current") = 3

  End Sub

</script>

      

In the example above, the system needs to know which week (out of 15 of them) that the current date is inside. Therefore, as soon as every Monday morning the value of "week_current" should change.

I can do this easily, but is there a way to give the user access to change this value without touching the code?

+1


a source to share


6 answers


Typical practice is to put them in a web.config file that you can edit. These values <appSettings>

will be available on every page, but the file can be edited.



+2


a source


I would consider using the built-in cache (System.Web.Caching.Cache)

This way you can store them wherever you want (say in a database), modify them easily from within the application, and retrieve them quickly and cheaply.



Internally a class that inherits BasePage uses the given Cache object (eg Cache.Add (..)) and from elsewhere uses HttpContext.Current.Cache (eg HttpContext.Current.Cache.Remove (Key))

+1


a source


1) It is usually good practice to store these values ​​in the web.config file. See here and here for some tutorials.

2) a) If you keep this in the web.config file, it will update easily without the need to recompile and the web application should get the changes immediately.

b) Does updating some type of "week number" really have to be a manual process? This sounds a bit buggy and I would suggest automating this, if at all possible, by calculating it based on the current date.

+1


a source


Other answers suggest different ways this can be done and should be done. But even if you want to allow the user to edit the global variable, you will need to lock or Mutex on the shared object, change the value of your global variable, and release the lock or Mutex.

lock(globalsharedobject) //This is C# code.
{
    Application("default_scoring_id") = 3;
}

      

0


a source


Web.config is the .NET or ASP.NET way, it is not always the most efficient or most appropriate.

You Global.asax file is much bigger than some events, you can put static data in any class of System.Web.HttpApplication subclass and inherit that in your Global.asax file.

HttpSessionState and HttpApplicationState are relics dating back to classic ASP time and you should avoid them because that's not the real purpose.

Depending on the type (System.Type) of your objects, you can create your own strongly typed objects that store information about your application and session, static fields are enough for application level data.

They need to be static because each HttpModule as well as an HttpApplication instance is a merged object, so to avoid this confusion, make sure your persistent data is stored in static or multiple static dicionaries. But be aware of concurrency issues when modifying these collections. A good strategy is to lock the object, only for the time you change it, and make sure you don't call any other code, while a modyfiny collection, a simple idiom swap, can be useful here, it's fast, and it's not -deadlock guarntee.

0


a source


<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
    // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e) 
    {
    //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e) 
    { 
    // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {

    // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised.

    }
    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    Context.Items.Add("Request_Start_Time", DateTime.Now);
    }
    protected void Application_EndRequest(Object sender, EventArgs e)
    {
    TimeSpan tsDuration = DateTime.Now.Subtract((DateTime)Context.Items["Request_Start_Time"]);
    Context.Response.Write("<b>Request Processing Time: From Global.asax file " + tsDuration.ToString());
    Application["time"] = tsDuration.ToString();
    }

</script>

      

0


a source







All Articles