6

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Anthony Levine
  • 61
  • 1
  • 1
  • 2
  • possible duplicate of [Cache VS Session VS cookies?](http://stackoverflow.com/questions/553185/cache-vs-session-vs-cookies) – Gabe Jul 20 '11 at 20:31
  • "And also, where/how is something declared if it is going to be used globally in a Web Application in a .net C# application?" You might want to clarify that question a bit. You can certainly define classes in a specific assembly, and reference that assembly when building ASP.NET websites, webservices, console apps, etc. HOWEVER, if you need disparate applications to share a specific INSTANCE of a class, then you probably need to look into .NET remoting or similar techniques. – mikemanne Jul 20 '11 at 21:29
  • A similar question was asked and I think this [answer](http://stackoverflow.com/questions/553185/cache-vs-session-vs-cookies/553196#553196) pretty much sums up all your options. – Samuel Béliveau Jul 20 '11 at 20:28

4 Answers4

7

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.

Overleaf
  • 710
  • 6
  • 10
Bill Martin
  • 4,825
  • 9
  • 52
  • 86
  • Personally I like this answer, it is very clean. I am going to use it. Thanks! – Mausimo Mar 16 '12 at 22:48
  • @Bill Martin - should the null check for Session["FooSessionObject"] not be within the get statement? get { if(Session["FooSessionObject"]==null) { //instantiate a new one Session["FooSessionObject"] = new Foo(); } return return (Foo)Session["FooSessionObject"]; – Mark Jul 07 '15 at 06:19
0

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.

0

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"];
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

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.

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32