Adsense

Friday, January 26, 2007

Session Management - Facade Pattern - C# .Net VS2005

State Management is a big part of any web application and using the session variable in ASP.NET is one way to persist data throughout a user’s session. Interacting with the session variable is fairly straight-forward…
Session["UserId"] = user.Id;
int id = (int)Session["UserId"];
Session[
"FirstName"] = user.FirstName;
string fName = (string)Session["FirstName"];
You can store any object or primitive into the session variable and retrieve the value at any time throughout the user’s session.

As you might imagine, your project could get fairly convoluted if you are accessing the same (or many different) session variable across many different pages throughout your application. Using explicit string values as your keys can also cause problems if you accidentally typo when entering them or decide to change one later. Throw several developers into the mix and this can turn into a nightmare.

You can alleviate some of this frustration by using an enum that contains all of your session key values. This allows you to make changes much easier, provides a quick reference to what objects you have in the session, and makes calls to the session variable consistent. The new session calls (with the help of an enum) would look like this.
enum SessionKeys {
UserId
= 1,
FirstName
= 2
}
Session[SessionKeys.UserId .ToString()]
= user.Id;
int id = (int)Session[SessionKeys.UserId .ToString()];
Session[SessionKeys.FirstName .ToString()]
= user.FirstName;
string fName = (string)Session[SessionKeys.FirstName .ToString()];
Taking this a step further, you could wrap this functionality into a static class in your project and route all session traffic through this class. By wrapping session calls in properties within this class, you can ensure type safety and make it much more easy to work with the session object.
public static class SessionFacade {
public static int UserId {
get {
return (int)Session[SessionKeys.UserId.ToString()];
}
set {
Session[SessionKeys.UserId.ToString()]
= value;
}
}
public static string FirstName {
get {
return (string)Session[SessionKeys.FirstName.ToString()];
}
set {
Session[SessionKeys.FirstName.ToString()]
= value;
}
}
}
Sessions calls would then look like this.
int id = SessionFacade.UserId;
SessionFacade.UserId
= user.Id;
string fName = SessionFacade.FirstName;
SessionFacade.FirstName
= user.FirstName;
The SessionFacade class acts as a Facade between you and the session object and alleviates much of the frustrations mentioned above. No more misspelled keys, no more huge updates if you want to change a key. Type safety... Who could ask for more?

3 comments:

Anonymous said...

use the same technique for the ViewState and the QueryString and you're good to go

Anonymous said...

it´s a good idea,
How does it work in asp.net?

Sorry by my english, but i try to learn.


The class can put in the app_data folder from the web site?

thanks

see you

Paul Fox said...

Yes. The SessionFacade would go with your other classes in the app_data. You could then use the SessionFacade class to interact with the Session instead of using the Session object directly.

Like the poster above said. You could use this same strategy to wrap the ViewState and QueryString to provide the same benefits.