Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Variables vs. ASP.NET Session State

This is probably going to sound rather naive, but I'm developing a web application that spans multiple pages. All of these pages instantiate the same class object w/ methods that accesses a CMS using their API. Currently, when a user starts creating content, I'm storing variables like a folder ID where the content is located in a Session variable.

My question is this: Can I instantiate a single instance of a class that can be used across all pages without having to do it on every page? If so, would each person accessing that page be given their own version of the class? I assume that using static variables and methods isn't the way to go since they are shared in memory. And also, where/how is something declared if it is going to be used globally in a Web Application in a .net C# application?

like image 238
Anthony Levine Avatar asked Jul 20 '11 20:07

Anthony Levine


People also ask

What is the difference between ASP session state and ASP.NET session state?

In Asp, the session is process dependent . That is, Asp session state is dependent on IIS process very heavily. So if IIS restarts Asp session variables are also recycled. Whereas In Asp.Net, the session is process independent .

Which is better session or ViewState?

The basic difference between these two is that the ViewState is to manage state at the client's end, making state management easy for end-user while SessionState manages state at the server's end, making it easy to manage content from this end too.

Is session a global variable?

Session global variables are either built-in global variables or user-defined global variables. The value of a database global variable is a single value that remains the same for all sessions that use this particular global variable. Database global variables are always built-in global variables.

What is difference between session and application variable?

Application variable: it is used for entire application which common for all users. It is not for user specific. this variable value will lost when application restarted. Session variable :- this variable is for user specific.


1 Answers

I recommend making a base class which inherits from System.Page. Then have your page code behind inherit from that.

Then, inside the base class, create a property that is a reference to your object. Like this, for example:

public class BasePage : System.Web.UI.Page
{
    public Foo CurrentFoo
    {
        get
        {
            return (Foo)Session["FooSessionObject"];
        }
        set
        {
            if(Session["FooSessionObject"] == null)
            { 
                // instantiate a new one
                Session["FooSessionObject"] = new Foo();
            }
            Session["FooSessionObject"] = value;
        }
    }
}

Then, from anywhere in any page, just do a CurrentFoo. and you will have access to any of the properties.

This makes for nice and clean code behind.

like image 52
Bill Martin Avatar answered Oct 14 '22 12:10

Bill Martin