Anthony Levine
Anthony Levine

Reputation: 61

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?

Upvotes: 6

Views: 16798

Answers (4)

Bill Martin
Bill Martin

Reputation: 4943

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.

Upvotes: 8

o0knight0o
o0knight0o

Reputation: 1

Bad thing about doing this and using global over sessions for website is the simple fact that global variables could be accessed by any user accessing the regardless of session. For example take the code below.

   public class globals
    {
        static string _test = "MyTest";
        public static string test 
        {
            get
             {
                return _test;
             }
            set
             {
                _test = value;
             }
        }

After I created the global class I added a label to my default from and assigned the global variable text to the label.

protected void Page_Load(object sender, EventArgs e)
    {
        globals.test = "Some Data";
    }

now you will see this on every form of you page however the down side to this is anyone else that accesses your page will also be able to see this data. If its data that needs to be shared across multiple sessions you could do this. If its data that just needs to be shared by the current user over multiple classes you need to use session variables.

Upvotes: 0

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

The pattern you are looking for is quite easy. There are a couple of ways to accomplish it.

  1. Create an object and store it in session
  2. Create a multi-part form and leave the items in viewstate

Each has its benefits.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

You should use Session state if you want to store information that is specific only to the current user.

For example in one page of the application you could store some value into the session:

SomeType someValue = ...
Session["someKey"] = someValue;

and then later on other page retrieve it from the session:

SomeType someValue = (SomeType)Session["someKey"];

Upvotes: 0

Related Questions