Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To follow on from what others have said. I tend to have two layers:</p> <p><strong>The core layer. This is within a DLL that is added to nearly all web app projects</strong>. In this I have a SessionVars class which does the grunt work for Session state getters/setters. It contains code like the following:</p> <pre><code>public class SessionVar { static HttpSessionState Session { get { if (HttpContext.Current == null) throw new ApplicationException("No Http Context, No Session to Get!"); return HttpContext.Current.Session; } } public static T Get&lt;T&gt;(string key) { if (Session[key] == null) return default(T); else return (T)Session[key]; } public static void Set&lt;T&gt;(string key, T value) { Session[key] = value; } } </code></pre> <p>Note the generics for getting any type.</p> <p>I then also add Getters/Setters for specific types, especially string since I often prefer to work with string.Empty rather than null for variables presented to Users.</p> <p>e.g:</p> <pre><code>public static string GetString(string key) { string s = Get&lt;string&gt;(key); return s == null ? string.Empty : s; } public static void SetString(string key, string value) { Set&lt;string&gt;(key, value); } </code></pre> <p>And so on...</p> <p><strong>I then create wrappers to abstract that away and bring it up to the application model. For example, if we have customer details:</strong></p> <pre><code>public class CustomerInfo { public string Name { get { return SessionVar.GetString("CustomerInfo_Name"); } set { SessionVar.SetString("CustomerInfo_Name", value); } } } </code></pre> <p>You get the idea right? :)</p> <p><strong>NOTE:</strong> Just had a thought when adding a comment to the accepted answer. Always ensure objects are serializable when storing them in Session when using a state server. It can be all too easy to try and save an object using the generics when on web farm and it go boom. I deploy on a web farm at work so added checks to my code in the core layer to see if the object is serializable, another benefit of encapsulating the Session Getters and Setters :)</p>
 

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