Saving Application Variables

There seem to be three different methods for storing a variable that will be available for every request in the application:

  • Global.asax.cs

    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            Application["SiteDatabase"] = new SiteDatabase();
        }
    }
    
  • Owin:

    public partial class Startup
    {
        public void ConfigureAuthentication(IAppBuilder Application)
        {
            Application.CreatePerOwinContext<SiteDatabase>(new SiteDatabase());
        }
    } 
    
  • Static container

    public static class GlobalVariables
    {
        private SiteDatabase _Database;
        public SiteDatabase Database
        {
            get { return _Database ?? new SiteDatabase(); }
        }
    } 
    

What are the relative advantages of each method?

+4
source share
1 answer

OK:

Global.asax.cs

  • Pros: works in all ASP.NET applications, is available statically, so you do not need to transfer data around the application yourself.
  • Cons: Not very typed. Does not work automatically in a distributed environment.

Owin

  • Pros: Works great in OWIN and flows through the app. Using statics is considered incorrect programming.
  • : , OWIN (, Katana). .

  • : ASP.NET, .
  • : :) .

  • :
  • : ; - ; . .

  • : , .
  • : ( ), , ASP.NET ( ).
+7

Source: https://habr.com/ru/post/1541808/


All Articles