views:

127

answers:

3

Ok, I've got this singleton-like web class which uses session to maintain state. I initially thought I was going to have to manipulate the session variables on each "set" so that the new values were updated in the session. However I tried using it as-is, and somehow, it remembers state.

For example, if run this code on one page:

UserContext.Current.User.FirstName = "Micah";

And run this code in a different browser tab, FirstName is displayed correctly:

Response.Write(UserContext.Current.User.FirstName);

Can someone tell me (prove) how this data is getting persisted in the session? Here is the class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class UserContext
{
  private UserContext() { }

  public static UserContext Current
  {
    get
    {
      if (System.Web.HttpContext.Current.Session["UserContext"] == null)
      {
        UserContext uc = new UserContext();
        uc.User = new User();
        System.Web.HttpContext.Current.Session["UserContext"] = uc;
      }

      return (UserContext)System.Web.HttpContext.Current.Session["UserContext"];
    }
  }

  private string HospitalField;
  public string Hospital
  {
    get { return HospitalField; }
    set
    {
      HospitalField = value;
      ContractField = null;
      ModelType = null;
    }
  }

  private string ContractField;
  public string Contract
  {
    get { return ContractField; }
    set
    {
      ContractField = value;
      ModelType = string.Empty;
    }
  }

  private string ModelTypeField;
  public string ModelType
  {
    get { return ModelTypeField; }
    set { ModelTypeField = value; }
  }

  private User UserField;
  public User User
  {
    get { return UserField; }
    set { UserField = value; }
  }

  public void DoSomething()
  {
  }
}

public class User
{
  public int UserId { get; set; }
  public string FirstName { get; set; }
}

I added this to a watch, and can see that the session variable is definitely being set somewhere:

(UserContext)System.Web.HttpContext.Current.Session["UserContext"];

As soon as a setter is called the Session var is immediately updated:

    set
    {
      HospitalField = value; //<--- here
      ContractField = null;
      ModelType = null;
    }
+2  A: 

The UserContextinstance is saved in Session with this line:

System.Web.HttpContext.Current.Session["UserContext"] = uc;

It's not a singleton. The static property UserContext will attempt to retrieve a instance from Session, and if it doesn't find it, create a new instance and store it in Session.

UPDATE

I can see how the session var is retrieved, my confusion is around how the session var is set.

To add clarification following Micah's comment: the first time the static Current property is accessed, a new UserContext instance is created, its User property is populated with a new User instance, and the UserContext instance is stored in Session. Subsequent accesses to UserContext.Current (and hence UserContext.Current.User) in the same session are all accessing the same instance.

If it's still not clear I suggest stepping through with a debugger.

public static UserContext Current 
{ 
    get 
    { 
        // If Session does not yet contain a UserContext instance ...
        if (System.Web.HttpContext.Current.Session["UserContext"] == null) 
        { 
            // ... then create and initialize a new UserContext instance ...
            UserContext uc = new UserContext(); 
            uc.User = new User(); 
            // ... and store it in Session where it will be available for
            // subsequent requests during the same session.
            System.Web.HttpContext.Current.Session["UserContext"] = uc; 
        } 
        // By the time we get here, Session contains a UserContext instance,
        // so return it.
        return (UserContext)System.Web.HttpContext.Current.Session["UserContext"]; 
    } 
} 
Joe
I can see how the session var is retrieved, my confusion is around how the session var is set. When I run UserContext.Current.User.FirstName = "Micah"; I don't see anywhere that it's getting put back into the session var. The only time the session is set is when it's creating a new instance with no data.
Micah Burnett
I'm not sure if you understand what I'm asking. As soon as the code calls "HospitalField = value;" the session goes from being null to being populated with a hospital name. Before that line executes, all fields in the session variable are null. Where is the session being set?The only place the code is setting the session var is when it's populated with a new object with empty fields.
Micah Burnett
When you call "UserContext.Current.Hospital = value;", the getter UserContext.Current is called first (and creates the object in Session), then the "Hospital" setter is called, by which time the object has already been created in Session. Have you tried stepping through with a debugger? You may need to change your VS Debug settings so that it steps into properties to see what's going on.
Joe
Thanks. This explanation with Nate's made it clear.
Micah Burnett
@Micah - I get the feeling, from your questions and confusion, that you are struggling to grasp the difference between a "property" and a "variable". A property *may* have a backing variable, but it does not *have* to have one. In this case, the backing storage for the property *is* the Session. A property does not have its own independent lifetime. Instead, it is a block of code that masquerades as a data member but hides the back-end implementation.
GalacticCowboy
A: 

And run this code in a different browser tab, FirstName is displayed correctly:

You are saving it in the session. Opening a new tab can use the same session information as the other tab (I'm not certain about all browsers). Try opening a new browser window (not just a tab), and see what happens.

Kevin
I get the same behavior with a new browser window.
Micah Burnett
My question is *how* is it getting saved in session.
Micah Burnett
+1  A: 

Joe is right. Your usage is "UserContext.Current.User.FirstName"

In the getter of UserContext.Current, you're getting back a reference to a piece of memory that lives inside the session object within asp.net. Using any of the setters should/would change that memory and if you inspect the session object either in the debugger or on subsequent lines of code, you should see the same data that you set with your setters.

Nate Jackson
That makes perfect sense. And it’s returned as a casted object so strongly typed properties can be set.
Micah Burnett