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.
--
Matt Berther
<a href="http://www.mattberther.com">http://www.mattberther.com</a>