title: Reduce global state in web applications
body: <p>There are numerous posts available on alternatives to global state, but I have lingering questions regarding implementation in a web environment.</p>
  
  <p>In ASP.NET, values stored in the session are practically global variables, but it seems you can't reasonably store, e.g., authenticated username, permissions, etc. across pages and events without resorting to the <code>Session[]</code> collection. (I would typically use dependency injection, but you wouldn't want to pass these values in the viewstate/url to the client.)</p>
  
  <p>So I take that global state is a necessary evil in web development (due to the stateless nature of HTTP) and the answer is to just use it responsibly. What is a good facade pattern for implementation? A static class such as the one below that just <code>get</code>s and <code>set</code>s values in the session doesn't do much to mask the global state.</p>
  
  <pre><code>public class SessionWrapper
  {
      public static string UserName
      {
          get { return Convert.ToString(Session["UserName"]); }
          set { Session["UserName"] = value; }
      }
      public static bool IsAuthenticated
      {
          get { return Convert.ToBoolean(Session["IsAuthenticated"]); }
          set { Session["IsAuthenticated"] = value; }
      }
  }
  </code></pre>