Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > typesafe session recomendation

Reply
Thread Tools

typesafe session recomendation

 
 
adam
Guest
Posts: n/a
 
      08-14-2003
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


 
Reply With Quote
 
 
 
 
Yan-Hong Huang[MSFT]
Guest
Posts: n/a
 
      08-15-2003
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" <(E-Mail Removed)>
!From: "adam" <(E-Mail Removed)>
!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: <(E-Mail Removed)>
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!NNTP-Posting-Host: 62.254.210.110
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.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
!
!
!


 
Reply With Quote
 
 
 
 
Yan-Hong Huang[MSFT]
Guest
Posts: n/a
 
      08-15-2003
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" <(E-Mail Removed)>
!From: "adam" <(E-Mail Removed)>
!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: <(E-Mail Removed)>
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!NNTP-Posting-Host: 62.254.210.110
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.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
!
!
!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
typesafe bitwise operations based on enums Daniel Gutson C++ 10 01-11-2008 02:52 PM
enum/generics typesafe getter for generic types visionset Java 7 04-02-2007 10:07 AM
Beginners Question: Typesafe collection with 1.5 scherer_mike@web.de Java 3 06-19-2006 09:17 PM
Question about Typesafe Enums Rhino Java 8 12-15-2005 01:18 AM
CheckBoxList and custom typesafe collection woes Matthias S. ASP .Net 3 03-30-2005 07:16 PM



Advertisments