title: Singleton in use in authentication
body: <p>I came across a Singleton implementation in a site I was tasked to make changes to. The only thing I know about Singleton is its definition and as such never had an opportunity to work "with" it. So I started to read articles and this one on <a href="http://www.yoda.arachsys.com/csharp/singleton.html" rel="nofollow">Implementing the Singleton Pattern in C#</a> nicely explains it. Except that the code I have looks not quite the same. Can some take a look at this and points out what this code does or how is it different?</p>
  
  <pre><code>public class Singleton&lt;TSingleton&gt; where TSingleton : class, new()
  {
      private static readonly Lazy&lt;TSingleton&gt; instance = new Lazy&lt;TSingleton&gt;(() =&gt; new TSingleton());
  
      public static TSingleton Instance { get { return instance.Value; } }
  }
  </code></pre>
  
  <p>This class is called like this <code>Singleton&lt;AccessLogger&gt;.Instance.LogAccess(accessData);</code> where <code>accessData</code> is an instance of AccessLoggerData and basically contains environment, page and user information. The accessData is an instance of AccessLoggerData class:</p>
  
  <pre><code>public class AccessLoggerData
  {
      public string Environment = Singleton&lt;ApplicationSettings&gt;.Instance.Environment;
      public string Page, UserId;
  
      public AccessLoggerData(string page, string user)
      {
          this.Page = page;
          this.UserId = user;
      }
  }
  </code></pre>
  
  <p>Strangely as I'm writing this post and somehow it is becoming more clear as to how these pieces come together, but one thing remains unclear, why would it be necessary to allow only one instance of this accessData object?</p>