jeudi 11 juin 2015

Are Session based Singleton Implementations plausible replacements for multiple Session Variables?

I have been considering a Singleton pattern design for a web application to replace the use of Session variables for reasons I'd rather not go into specific detail about; but after researching the topic it seems as though this pattern is probably not the most efficient or effective approach.

So I am now considering an approach that would effectively reap some of the benefits of the Singleton design in combination with the use of Session variables.

The question is as stated, "Are Session based Singleton Implementations plausible replacements for multiple Session Variables"

Hopefully this question is not taken a matter of opinion but rather a matter of fact based on the concepts of Session variables and Singleton pattern designs.

My implementation would look similar to the following:

public class SingletonSessionData
{
   // User Identity Properties
   public string UserId { get; set; }
   public string UserName { get; set; }

   private const string SessionData= "UserSessionObject";
   private SingletonSessionData() { }

   public static SingletonSessionData Instance()
   {
       SingletonSessionData oSingleton;
       if(null == System.Web.HttpContext.Current.Session[SessionData])
       {
           // use private constructor to create an instance, place it into the session
           oSingleton = new SingletonSessionData();
           System.Web.HttpContext.Current.Session[SessionData] = oSingleton;
       }
       else
       {
           //Retrieve the existing instance 
           oSingleton = (ReportSessionData)System.Web.HttpContext.Current.Session[ReportSessionData];
       }
       return oSingleton;
   }
}

What I am expecting to gain from this approach is to still maintain the persistence of session variables to a specific session but have the benefit of use with Intellisense and what I believe would potentially be more efficient and effective memory management by having the Singleton exist in the session.

Reasonable or no?

Aucun commentaire:

Enregistrer un commentaire