typesafe session recomendation

A

adam

Having spent nearly 2 years in win forms land the inevitable request came
for me to "do some web pages".
So being new to this bit of .net and having had a look around I can't see
where the best way to store session data.

1) use the (string)Session["MyValue"] approach
Obviously bad for maintenance, readability etc.

2) have a typed MySession object with static properties for each value I
want to store
class MySession
{
public static string MyValue
{
get{return (string)Session["MyValue"];}
set{Session["MyValue"] = value;}
}
}

3) have a single session object that has all properties on it
class MySession
{
string m_MyValue = "";
public static MySession GetSession(HttpSession sess)
{
MySession ret = sess["MySession"] as MySession;
if (ret==null)
{
ret = new MySession();
sess["MySession"] = ret;
}
return ret;
}
public string MyValue
{
get{return m_MyValue ;}
set{m_MyValue = value;}
}
}
4) do as for 3 but for each Page (or a base Page) for page specific
information

all session key strings can of course be const or an enum.

What is the recommended approach (ignore scaling to multiple servers for
now)?

adam
 
Y

Yan-Hong Huang[MSFT]

Hello Adam,

Thanks for posting in the group.

This is really a good question. The Application and Session objects provide convenient containers for caching data in
memory. You can assign data to both Application and Session objects, and this data will remain in memory between HTTP
calls. Session data is stored per user, while Application data is shared between all users.

When you store data in Application or Session scope, the data will remain there until you programmatically change it, the
Session expires, or the Web application is restarted.

Be aware that caching large arrays in Session or Application objects is not a good idea. Before you can access any
element of the array, the semantics of the scripting languages require that a temporary copy of the entire array be made. For
example, if you cache a 100,000-element array of strings that maps U.S. zip codes to local weather stations in the Application
object, ASP must first copy all 100,000 weather stations into a temporary array before it can extract just one string. In this
case, it would be much better to build a custom component with a custom method to store the weather stations¡ªor to use one
of the dictionary components.

One more comment in the spirit of not throwing out the baby with the bath water: Arrays provide fast lookup and storage of
key-data pairs that are contiguous in memory. Indexing a dictionary is slower than indexing an array. You should choose the
data structure that offers the best performance for your situation

Besides, as an alternative to using the Session object, there are many options for managing Session state. For small
amounts of state (less than 4 KB), we usually recommend using Cookies, QueryString variables, and hidden-form variables.
For larger amounts of data such as shopping carts, a back-end database is the most appropriate choice. A lot has been
written about state-management techniques in Web server farms. See the following references for more details.

Q175167: HOWTO: Persisting Values Without Sessions
Q157906: HOWTO: How To Maintain State Across Pages with VBScript
XML-based Persistence Behaviors Fix Web Farm Headaches by Aaron Skonnard
MSDN Magazine: Basic Instinct To Cache or not to Cache by Ted Pattison

Hope them help.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!Reply-To: "adam" <[email protected]>
!From: "adam" <[email protected]>
!Subject: typesafe session recomendation
!Date: Thu, 14 Aug 2003 10:11:34 +0100
!Lines: 50
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <[email protected]>
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!NNTP-Posting-Host: 62.254.210.110
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:167785
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!
!Having spent nearly 2 years in win forms land the inevitable request came
!for me to "do some web pages".
!So being new to this bit of .net and having had a look around I can't see
!where the best way to store session data.
!
!1) use the (string)Session["MyValue"] approach
!Obviously bad for maintenance, readability etc.
!
!2) have a typed MySession object with static properties for each value I
!want to store
!class MySession
!{
! public static string MyValue
! {
! get{return (string)Session["MyValue"];}
! set{Session["MyValue"] = value;}
! }
!}
!
!3) have a single session object that has all properties on it
!class MySession
!{
! string m_MyValue = "";
! public static MySession GetSession(HttpSession sess)
! {
! MySession ret = sess["MySession"] as MySession;
! if (ret==null)
! {
! ret = new MySession();
! sess["MySession"] = ret;
! }
! return ret;
! }
! public string MyValue
! {
! get{return m_MyValue ;}
! set{m_MyValue = value;}
! }
!}
!4) do as for 3 but for each Page (or a base Page) for page specific
!information
!
!all session key strings can of course be const or an enum.
!
!What is the recommended approach (ignore scaling to multiple servers for
!now)?
!
!adam
!
!
!
 
Y

Yan-Hong Huang[MSFT]

Hello Adam,

For your question,
3), 4) may cost some unnecessary resource
2) is better than 1) for maintaining if you have too many session variables.

Thanks.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!Reply-To: "adam" <[email protected]>
!From: "adam" <[email protected]>
!Subject: typesafe session recomendation
!Date: Thu, 14 Aug 2003 10:11:34 +0100
!Lines: 50
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <[email protected]>
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!NNTP-Posting-Host: 62.254.210.110
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:167785
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!
!Having spent nearly 2 years in win forms land the inevitable request came
!for me to "do some web pages".
!So being new to this bit of .net and having had a look around I can't see
!where the best way to store session data.
!
!1) use the (string)Session["MyValue"] approach
!Obviously bad for maintenance, readability etc.
!
!2) have a typed MySession object with static properties for each value I
!want to store
!class MySession
!{
! public static string MyValue
! {
! get{return (string)Session["MyValue"];}
! set{Session["MyValue"] = value;}
! }
!}
!
!3) have a single session object that has all properties on it
!class MySession
!{
! string m_MyValue = "";
! public static MySession GetSession(HttpSession sess)
! {
! MySession ret = sess["MySession"] as MySession;
! if (ret==null)
! {
! ret = new MySession();
! sess["MySession"] = ret;
! }
! return ret;
! }
! public string MyValue
! {
! get{return m_MyValue ;}
! set{m_MyValue = value;}
! }
!}
!4) do as for 3 but for each Page (or a base Page) for page specific
!information
!
!all session key strings can of course be const or an enum.
!
!What is the recommended approach (ignore scaling to multiple servers for
!now)?
!
!adam
!
!
!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top