472,353 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

typesafe session recomendation

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
Nov 17 '05 #1
2 1601
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" <no****@msdn-beacon-ct.co.uk>
!From: "adam" <no****@msdn-beacon-ct.co.uk>
!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: <ef**************@TK2MSFTNGP12.phx.gbl>
!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
!
!
!
Nov 17 '05 #2
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" <no****@msdn-beacon-ct.co.uk>
!From: "adam" <no****@msdn-beacon-ct.co.uk>
!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: <ef**************@TK2MSFTNGP12.phx.gbl>
!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
!
!
!
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gerry Viator | last post by:
Hi all, Can someone recommend a good 3rd party image\video control or VS.NET control for imaging\video. I will need a recomendation on a good...
3
by: Matthias S. | last post by:
Hi there, I have a typesafe collection in my application called Countries, which is derived from the ReadOnlyCollectionBase. As you might guess,...
3
by: Saeid | last post by:
Hi, I want to buy a book about .net Webservices, does any body have a suggestion? Thanks ------------------------ The soldier of Persian Empire
4
by: Davo | last post by:
I wish to setup a resource that holds CONSTANT values. The resource file is call LogCat.resx (Logger Categories) and contains Debug, Info, Error...
1
by: Luis Arvayo | last post by:
Hi, I hope this is the correct place for this post. What is your favourite graphic tool for building the glyphs/images/icons on toolbars ?....
14
by: aroraamit81 | last post by:
Hi, I am facing a trouble. I have some Session variables in my code and somehow my session variables are getting mixed up with other users. ...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use...
8
by: Michael Schwarz | last post by:
Hi, I have a problem where I have two requests (i.e. two different windows that are using the same session) that are accessing the session...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful,...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.