Session Variables in Static Objects

J

james

Hi

I am considering storing my session variables within one static object
with session scope. The static object will be a class with accessor
functions to get and set the equivalent session variables stored as
member variables. This will allow strong typing. Apart from the
problems of multithreading, are there any performance overheards of
which I should be aware?

Thanks

James
 
K

Karl Seguin [MVP]

The way this is normally accomplished is via something like:

public class User
{
private int _userId;
public int UserId { get { return _userId; } set { _userId = value;} }

private User(){}

public static User GetCurrentUser()
{
get
{
if (HttContext.Current != null &&
HttpContext.Current.Session["User"] != null)
{
return (User)HttContext.Current.Session["User"];
}
//throw an error? return an anonymous user?
}
}
}

This avoids multithreadng issues, and doesn't have any performance
drawbacks.

Karl
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top