Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>UserContext</code>instance is saved in Session with this line:</p> <pre><code>System.Web.HttpContext.Current.Session["UserContext"] = uc; </code></pre> <p>It's not a singleton. The static property <code>UserContext</code> will attempt to retrieve a instance from Session, and if it doesn't find it, create a new instance and store it in Session.</p> <p><strong>UPDATE</strong></p> <blockquote> <p>I can see how the session var is retrieved, my confusion is around how the session var is set.</p> </blockquote> <p>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 <code>UserContext.Current</code> (and hence <code>UserContext.Current.User</code>) in the same session are all accessing the same instance.</p> <p>If it's still not clear I suggest stepping through with a debugger.</p> <pre><code>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"]; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload