473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 1673
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 capture card as well for medical images\video?...
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, the item-value is of type Country. Each Country...
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 etc... This resource is located in a resource...
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 ?. Photoshop, PaintShop Pro, or ?. I mean 16x16 or...
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. For example User A has access to 10 companies and...
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 standard enums? There's now a nice little code...
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 collection. The requests will need more time on the...
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, so hopefully it'll be for somebody else as well....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.