In this article we will try to understand how to make ASP.Net session objects Type Safe and override safety.OOPObject Oriented Programming - It just means reusability and making things more manageable.All the architectures and technologies are just based on them or we can say derived from them.ASP.NET SessionASP.Net Session objects are basically used to store user-specific data, data which will be needed in more than one page related to a user.Problem StatementA company ABC organization is working on an E-Commerce website with around 5 developers. Each developer is working on one module.
SolutionMake project architecture in such a way that for sessions there will be a central repository, if anybody wants to access or modify session values they go through that repository. For that create a Class SessionManager as follows:public class ClsStateManagement{
public string UserId {
get {
if (HttpContext.Current.Session["UserId"] == null)
return null;
return HttpContext.Current.Session["UserId"].ToString();
}
set {
HttpContext.Current.Session["UserId"] = value;
public int TotalPrice {
if (HttpContext.Current.Session["TotalPrice"] == null)
return 0; return int.Parse(HttpContext.Current.Session["TotalPrice"].ToString());
set
{
HttpContext.Current.Session["TotalPrice"] = value; }
}We wrapped all session data and hid all the unnecessary things from the external world; that's called encapsulation.Hope you enjoyed the article, make sure to leave some good comments.