Session Wrapper Design Pattern For ASP.NET Core

In previous post, we learned how to use Session in ASP.NET Core. In this article, we'll learn about Session Wrapper design pattern to ease the access of Session. In short, we'll make our access of session "Typed". Also, we may apply any validation or constraint in this wrapper class.
 
Step 1 - Create a Session Manager class
 
In this example, we are going to store two items in Session (i.e. ID & LoginName).
  1. We are injecting IHttpContextAccessor so that we can access the Session variable. 
  2. We are creating properties which are actually accessing Session variable and returning the data or writing the data to Session.
  3. We have added one helping property "IsLoggedIn" which is using other properties to make a decision. We may have more such helping/wrapper properties.
  1. using Microsoft.AspNetCore.Http;

  2. public class SessionManager  
  3.     {  
  4.         private readonly ISession _session;  
  5.         private const String ID_KEY = "_ID";  
  6.         private const String LOGIN_KEY = "_LoginName";  
  7.         public SessionManager(IHttpContextAccessor httpContextAccessor)  
  8.         {  
  9.             _session = httpContextAccessor.HttpContext.Session;  
  10.         }  
  11.   
  12.         public int ID  
  13.         {  
  14.             get  
  15.             {  
  16.                 var v = _session.GetInt32(ID_KEY);  
  17.                 if (v.HasValue)  
  18.                     return v.Value;  
  19.                 else  
  20.                     return 0;  
  21.             }  
  22.             set  
  23.             {  
  24.                 _session.SetInt32(ID_KEY, value);  
  25.             }  
  26.         }  
  27.         public String LoginName  
  28.         {  
  29.             get  
  30.             {  
  31.                 return _session.GetString(LOGIN_KEY);  
  32.             }  
  33.             set  
  34.             {  
  35.                 _session.SetString(LOGIN_KEY, value);  
  36.             }  
  37.         }  
  38.         public Boolean IsLoggedIn  
  39.         {  
  40.             get  
  41.             {  
  42.                 if (ID > 0)  
  43.                     return true;  
  44.                 else  
  45.                     return false;  
  46.             }  
  47.         }  
  48.     }  

Step 2

Registering IHttpContextAccessor and SessionManager in Startup file.
  1. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();  
  2. services.AddScoped<SessionManager>();  
Step 3
 
Injecting SessionManager in your classes. Here is an example of Controller class but in the same way, it can be injected in non-controller classes too.
  1. private readonly SessionManager _sessionManager;  
  2. public HomeController(SessionManager sessionManager)  
  3. {  
  4.       _sessionManager = sessionManager;  
  5. }  
Step 4
 
Using SessionManager to access Session Data,
  1. _sessionManager.ID = 1;  
  2. _sessionManager.LoginName = dto.Login;  
  1. if(_sessionManager.IsLoggedIn == true)  
  2. {  
  3.    ViewBag.Login = _sessionManager.LoginName;  
  4.    return View();  
  5. }  

Conclusion

 
This wrapper pattern helps using Session without worrying about KeyNames & Makes access easier. It also helps you apply different conditioning and constraints in a wrapper class.