Thursday, December 17, 2015

Using HttpContext.Current.Application to Store Data

We do database CRUD operations all the time in our applications. And some are expensive and some are not. But when it comes to Web Applications no matter the size, accessing database frequently is really affecting to the performance of the web site.

Usually for global data access such as application tokens, we can store them as Application objects to be retrieved as and when needed irrespective of the users who log in to the website. For example, look at the below method.

 public void InitializeTokens()  
     {  
       List<TokenEntity> tokenList = new List<TokenEntity>();  
       tokenList = new TokenDAL().GetTokens();  
       foreach (var item in tokenList)  
       {          
         HttpContext.Current.Application[item.TokenId].ToString().ToUpper()] = item.TokenName.ToString();  
       }  
     }  

In InitializeTokens() method we get all the tokens from the database through TokenDAL database class and add them to HttpContext.Current.Application object.

 HttpContext.Current.Application.Get(token.TokenId).ToString();  

The above code will easily get the value of the token by its id stored before. Using OOP concepts, we can use this same methods to be used across the application so that accessing common data makes faster than before.





No comments:

Post a Comment