<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Type Safe Session Variables</title>
	<atom:link href="http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/</link>
	<description>ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.</description>
	<lastBuildDate>Thu, 11 Mar 2010 14:34:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Donny V</title>
		<link>http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/comment-page-1/#comment-3758</link>
		<dc:creator>Donny V</dc:creator>
		<pubDate>Fri, 09 Oct 2009 18:32:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/#comment-3758</guid>
		<description>I&#039;v been doing this for a while now.
One thing I learned in doing this is that in large projects you want
to use a GUID as the session variable. I usually  don&#039;t have 1 large session data class. I use it in any class. So you may accidentally use the same session variable. Using the GUID prevents that from happening.

Also you might want to think about some type of rehydration code inside the property. So that when IIS decides to dump the session state in the middle of your code executing it can rehydrate that session from nothing.

Here is an example

string UserAccountSessionID = &quot;88766165-b250-4d65-af10-5f2b6c9eca0d&quot;;
public static UserAccount GetUserAccount
{
	get
	{
		System.Web.SessionState.HttpSessionState SessionState = System.Web.HttpContext.Current.Session;
		UserAccount UA = null;
		UA = SessionState[UserAccountSessionID] as UserAccount;
		if (UA == null)
		{
			UA = GetUserAccount(CurrentUserID);
			SessionState[UserAccountSessionID] = UA;
		}
		
		return UA;
	}
}</description>
		<content:encoded><![CDATA[<p>I&#8217;v been doing this for a while now.<br />
One thing I learned in doing this is that in large projects you want<br />
to use a GUID as the session variable. I usually  don&#8217;t have 1 large session data class. I use it in any class. So you may accidentally use the same session variable. Using the GUID prevents that from happening.</p>
<p>Also you might want to think about some type of rehydration code inside the property. So that when IIS decides to dump the session state in the middle of your code executing it can rehydrate that session from nothing.</p>
<p>Here is an example</p>
<p>string UserAccountSessionID = &#8220;88766165-b250-4d65-af10-5f2b6c9eca0d&#8221;;<br />
public static UserAccount GetUserAccount<br />
{<br />
	get<br />
	{<br />
		System.Web.SessionState.HttpSessionState SessionState = System.Web.HttpContext.Current.Session;<br />
		UserAccount UA = null;<br />
		UA = SessionState[UserAccountSessionID] as UserAccount;<br />
		if (UA == null)<br />
		{<br />
			UA = GetUserAccount(CurrentUserID);<br />
			SessionState[UserAccountSessionID] = UA;<br />
		}</p>
<p>		return UA;<br />
	}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/comment-page-1/#comment-3708</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Wed, 07 Oct 2009 19:26:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/#comment-3708</guid>
		<description>Personally, I&#039;d rather get the error and find out quickly that the session was misused.</description>
		<content:encoded><![CDATA[<p>Personally, I&#8217;d rather get the error and find out quickly that the session was misused.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Holodak</title>
		<link>http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/comment-page-1/#comment-3707</link>
		<dc:creator>Scott Holodak</dc:creator>
		<pubDate>Wed, 07 Oct 2009 19:22:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/#comment-3707</guid>
		<description>Rather than casing the object, consider using the &#039;as&#039; keyword in your get blocks.  &#039;as&#039; will return null if the object can&#039;t be cast to the desired type instead of throwing an InvalidCastException.  Even though you are providing a class to interface with the Session object in a strongly-typed way, it&#039;s still possible for other developers to short circuit your system and manipulate the Session object directly, so it is possible to get some object type of the Session object that your code doesn&#039;t expect.  I think you&#039;d rather get a null value back in that case than an unhandled exception.</description>
		<content:encoded><![CDATA[<p>Rather than casing the object, consider using the &#8216;as&#8217; keyword in your get blocks.  &#8216;as&#8217; will return null if the object can&#8217;t be cast to the desired type instead of throwing an InvalidCastException.  Even though you are providing a class to interface with the Session object in a strongly-typed way, it&#8217;s still possible for other developers to short circuit your system and manipulate the Session object directly, so it is possible to get some object type of the Session object that your code doesn&#8217;t expect.  I think you&#8217;d rather get a null value back in that case than an unhandled exception.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jef Claes</title>
		<link>http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/comment-page-1/#comment-3706</link>
		<dc:creator>Jef Claes</dc:creator>
		<pubDate>Wed, 07 Oct 2009 17:45:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/#comment-3706</guid>
		<description>I have been doing this for some time now. 

Good thing you blogged about it!</description>
		<content:encoded><![CDATA[<p>I have been doing this for some time now. </p>
<p>Good thing you blogged about it!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ira</title>
		<link>http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/comment-page-1/#comment-3702</link>
		<dc:creator>Ira</dc:creator>
		<pubDate>Wed, 07 Oct 2009 15:02:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dmbcllc.com/2009/10/07/type-safe-session-variables/#comment-3702</guid>
		<description>I have always found it a little easier to type things going in and coming out of the session using a session helper class:

/// 
/// Helper object for strongly typed access to the session.
/// 
public class SessionHelper
{

	#region Methods (2) 


	// Public Methods (2) 

	/// 
	/// Gets the session value.
	/// 
	/// 
	/// The key.
	/// Returns a 
	public static T GetSessionValue(string key)
	{
		T result = default(T);
		if (HttpContext.Current.Session[key] != null)
			result = (T)HttpContext.Current.Session[key];
		return result;
	}

	/// 
	/// Sets the session value.
	/// 
	/// 
	/// The key.
	/// The value.
	public static void SetSessionValue(string key, T value)
	{
		if (HttpContext.Current.Session[key] == null)
			HttpContext.Current.Session.Add(key, value);
		else
			HttpContext.Current.Session[key] = value;
	}


	#endregion Methods 

}</description>
		<content:encoded><![CDATA[<p>I have always found it a little easier to type things going in and coming out of the session using a session helper class:</p>
<p>///<br />
/// Helper object for strongly typed access to the session.<br />
///<br />
public class SessionHelper<br />
{</p>
<p>	#region Methods (2) </p>
<p>	// Public Methods (2) </p>
<p>	///<br />
	/// Gets the session value.<br />
	///<br />
	///<br />
	/// The key.<br />
	/// Returns a<br />
	public static T GetSessionValue(string key)<br />
	{<br />
		T result = default(T);<br />
		if (HttpContext.Current.Session[key] != null)<br />
			result = (T)HttpContext.Current.Session[key];<br />
		return result;<br />
	}</p>
<p>	///<br />
	/// Sets the session value.<br />
	///<br />
	///<br />
	/// The key.<br />
	/// The value.<br />
	public static void SetSessionValue(string key, T value)<br />
	{<br />
		if (HttpContext.Current.Session[key] == null)<br />
			HttpContext.Current.Session.Add(key, value);<br />
		else<br />
			HttpContext.Current.Session[key] = value;<br />
	}</p>
<p>	#endregion Methods </p>
<p>}</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.207 seconds -->
