title: Static Singleton In ASP.Net Session
body: <p>I've been reading about singletons in ASP.Net and I've seen various implementations and suggestions. I've tried to model my implementation after this one: <a href="https://stackoverflow.com/questions/2134511/asp-net-singleton">https://stackoverflow.com/...asp-net-singleton</a></p>
  
  <p>Here is my question: I would like the object that I'm instantiating to last the life of the current session, but not be shared between sessions. For example, if two users are logged in at the same time, I want them to each "own" an instance of a global object. Below is my implementation. Is this the proper way to do this?</p>
  
  <pre><code>public class AppGlobal
  {
      #region Contructors
  
      public AppGlobal() { }
  
      public static AppGlobal Instance
      {
          get
          {
              HttpSessionState session = HttpContext.Current.Session;
  
              if (session["AppGlobalInstance"] == null)
              {
                  session["AppGlobalInstance"] = new AppGlobal();
              }
  
              return (AppGlobal)session["AppGlobalInstance"];
          }
      }
  
      #endregion
  
      #region Public Properties
  
      public User UserObject { get; set; }
      public Campaign CampaignObject { get; set; }
      public List&lt;int&gt; SelectedContactIDs = new List&lt;int&gt;();
      public List&lt;int&gt; UnsubmittedContactIDs = new List&lt;int&gt;();
      public List&lt;int&gt; SubmittedContactIDs = new List&lt;int&gt;();
      public List&lt;int&gt; ProcessedContactIDs = new List&lt;int&gt;();
  
      #endregion
  
      #region Public Instance Methods
  
      public void ClearCampaign()
      {
          CampaignObject = null;
          UnsubmittedContactIDs.Clear();
          SubmittedContactIDs.Clear();
          ProcessedContactIDs.Clear();
          SelectedContactIDs.Clear();
      }
      public void LoadCampaign(int campaignID)
      {
          //Ensure that old data is overwritten
          MailCampaignManagerEntities db = new MailCampaignManagerEntities();
  
          db.Campaigns.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
  
          //Clear the campaign and associated data
          ClearCampaign();
  
          //Set campaign object in AppGlobal
          this.CampaignObject = db.Campaigns.SingleOrDefaultasp.net(x =&gt; x.CampaignID == campaignID);
  
          //Populate Contact Status Lists
          this.UnsubmittedContactIDs.AddRange(from x in this.CampaignObject.CampaignContacts
                                                   where x.ContactSubmissionID == null
                                                   select x.CampaignContactID);
  
          this.SubmittedContactIDs.AddRange(from x in this.CampaignObject.CampaignContacts
                                                 where x.ContactSubmissionID != null
                                                 select x.CampaignContactID);
  
          this.ProcessedContactIDs.AddRange(from x in this.CampaignObject.CampaignContacts
                                                 where x.ContactSubmissionID != null
                                                 &amp;&amp; x.ContactSubmission.DateProcessed != null
                                                 select x.CampaignContactID);
      }
  
      #endregion
  
      #region Public Static Methods
  
      public static void WriteLogEntry(int? campaignID, int? contactSubmissionID, int? scheduledDropID, int? userID, string activityDescription)
      {
          ActivityLog activityLog = new ActivityLog();
          activityLog.CampaignID = campaignID;
          activityLog.ContactSubmissionID = contactSubmissionID;
          activityLog.ScheduledDropID = scheduledDropID;
          activityLog.UserID = userID;
          activityLog.Text = activityDescription;
          activityLog.CreatedDate = DateTime.Now;
  
          using (MailCampaignManagerEntities db = new MailCampaignManagerEntities())
          {
              db.ActivityLogs.AddObject(activityLog);
              db.SaveChanges();
          }
      }
  
      #endregion
  }
  </code></pre>