Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Application Variables

There seems to be three distinct methods of storing a variable that will be available for every request in an application:

  1. Global.asax.cs

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

    public partial class Startup
    {
        public void ConfigureAuthentication(IAppBuilder Application)
        {
            Application.CreatePerOwinContext<SiteDatabase>(new SiteDatabase());
        }
    } 
    
  3. 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?

like image 735
William Avatar asked May 23 '14 17:05

William


People also ask

How do you store variables?

To store a value in a variableUse the variable name on the left side of an assignment statement. The following example sets the value of the variable alpha . The value generated on the right side of the assignment statement is stored in the variable.

What are application variables?

Application variables are values that are available to any user or page within an application. For the first time, they can be any value you wish to store.

How do you store local variables that can be accessed within the application?

We can store local variables that can be accessed within the application by using app. locals.

How will you store a value in application variable and session variable?

application variable creates only one memory for one variable and for all user . while session creates one memory for one variable for one user :) Application variable will be maintained like global variable. Ex if I create a application session in my project and assign this value 1.

Which variables are used for storing values?

Answer: Variables are used for storing values such as strings, integers, list, dictionary etc.

Can you store anything in a variable?

One special thing about variables is that they can contain just about anything — not just strings and numbers. Variables can also contain complex data and even entire functions to do amazing things.


1 Answers

In order:

Global.asax.cs

  • Pros: Works in all ASP.NET apps, available statically so you don't have to flow the data around the app yourself.
  • Cons: Not strongly-typed. Doesn’t automatically work in distributed environment.

OWIN

  • Pros: Works great in OWIN and flows across the app. Using statics is considered poor programming by some.
  • Cons: Doesn't work if you're not using OWIN (e.g. Katana). Doesn’t automatically work in distributed environment.

Static Container

  • Pros: Works in all ASP.NET apps, available statically. Strongly-typed means no ugly casts
  • Cons: It's static :) Doesn’t automatically work in distributed environment.

Database

  • Pros: Works in a distributed system
  • Cons: Potentially very slow; can fail due to network issues; requires a lot more code to implement reliably. The data must also be serializable.

Cache

  • Pros: Similar to database, except much faster.
  • Cons: Similar to database (but much faster), plus the cache isn't generally meant for long-lived objects, though the ASP.NET cache does offer that behavior (but at that point, why use the cache).
like image 91
Eilon Avatar answered Oct 07 '22 15:10

Eilon