User & application-state variables...

J

Jeremy

I am relativly new to ASP.NET, and my question is this:

Is it better to create a wrapper class that contains all the running
settings for the application and then load that into the app state, or is it
better to load the scalar variables and store them individually?

In case I'm not making my self clear, is it better to do this:

'// Load each variable individually
Context.Cache.Add( "UserFirstName", txtFirstName.Text ... )
Context.Cache.Add( "UserLastName", txtLastName.Text ... )

or this:

'// Store vars in a class, and then cache the class
Config.ActiveUser.FirstName = txtFirstName.Text
Config.ActiveUser.LastName = txtLastName.Text

Context.Cache.Add( "Config", Config ... )

?

Thanks,
Jeremy
 
S

Steve C. Orr [MVP, MCSD]

Both ways are valid. It's all about tradeoffs.
The first way is simpler, the second way allows for more custom
functionality.
Simplicity vs. Power is the kind of decision we developers face nearly every
day.
You must decide which way is best for your app.
 
M

Matt Berther

Hello Jeremy,
Is it better to create a wrapper class that contains all the running
settings for the application and then load that into the app state, or
is it better to load the scalar variables and store them individually?

My answer is going to be geared toward session, however, I think that you'll be able to apply this anywhere.

Typically, the first thing that I do when I start up a web project that requires session state is implement a wrapper class for it that looks like this:

class SessionManager
{
private const string SESSION_MANAGER = "SESSION_MANAGER";
private string username;

private SessionManager()
{
}

public string UserName
{
get { return username; }
set { username = value; }
}

public static SessionManager Instance
{
SessionManager manager = HttpContext.Current.Session[SESSION_MANAGER] as SessionManager;
if (manager == null)
{
manager = new SessionManager();
HttpContext.Current.Session[SESSION_MANAGER] = manager;
}
return manager;
}
}

Using this class is very simple. All you need to do is add property accessors to it for items that you wish to store in session, as I have done in the above example with the UserName property.

When I want to access the session state in the code, I type
SessionManager.Instance.UserName = "someValue"; or
string someValue = SessionManager.Instance.UserName;

What I have now is a singleton pattern that exposes any session state that I need in a strongly typed fashion (ie: no longer do I run the risk of accidentally typing in the wrong string when I do Session["someValue"]). I can also control whether or not a property is read only at compile time.

I hope this answers your question.
 
S

Scott Allen

As another follow up -

Be careful not to put user specific information (like first and last
name) into the cache without some unique per user identifier (or
consider using the Session object). Otherwise two different users may
see the same name pulled from the cache when they come to your site.
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top