title: How to ensure that my class Initialize method is called only once, what would be the best approach?
body: <p>I'm currently using Unity IoC Container and here is my AppConfig class. As you can see the Initialize method should be called only once and I have used double lock checking to ensure that.</p>
  
  <p>What would be the best way to implement achieve this if my approach is not the best way?</p>
  
  <pre><code>public interface IAppConfig
  {
      /// &lt;summary&gt;
      /// Gets the admin username.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The admin username.&lt;/value&gt;
      string AdminUsername { get; }
  
      /// &lt;summary&gt;
      /// Gets the admin password.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The admin password.&lt;/value&gt;
      string AdminPassword { get; }
  
      /// &lt;summary&gt;
      /// Initializes this instance.
      /// &lt;/summary&gt;
      void Initialize();
  }
  
  /// &lt;summary&gt;
  /// A singleton App config which helps reading from web.config
  /// its lifetime is controlled by Unity.
  /// &lt;/summary&gt;
  public class AppConfig : IAppConfig
  {
      #region Fields
  
      /// &lt;summary&gt;
      /// the injectable config manager
      /// &lt;/summary&gt;
      private readonly IConfigManager _configManager;
  
      private readonly ILogger _logger;
  
      private static readonly object LockObject = new object();
  
      private static bool _initialized = false;
  
      #endregion
  
      #region Constructors
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="AppConfig"/&gt; class.
      /// &lt;/summary&gt;
      public AppConfig(IConfigManager configManager, ILogger logger)
      {
          this._configManager = configManager;
          this._logger = logger;
      }
  
      #endregion
  
      #region Properties
  
      /// &lt;summary&gt;
      /// Gets the admin username.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The admin username.&lt;/value&gt;
      public string AdminUsername { get; private set; }
  
      /// &lt;summary&gt;
      /// Gets the admin password.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The admin password.&lt;/value&gt;
      public string AdminPassword { get; private set; }
  
      #endregion
  
      #region Methods
  
      public void Initialize()
      {
          if (_initialized)
          {
              throw new ApplicationException("Initialize method should be called only once");
          }
  
          lock(LockObject)
          {
              if (_initialized) return;
  
              var adminUserNameSetting = _configManager.AppSettings[ConfigKeys.AdminUsername];
  
              if (adminUserNameSetting == null)
              {
                  throw new ApplicationException("AdminUsername key not found");
              }
  
              this.AdminUsername = adminUserNameSetting.Value;
  
              if (String.IsNullOrWhiteSpace(this.AdminUsername))
              {
                  _logger.LogError("AdminUsername not found");
              }
  
              // log
              var adminPasswordSetting = _configManager.AppSettings[ConfigKeys.AdminPassword];
  
              if (adminPasswordSetting == null)
              {
                  throw new ApplicationException("AdminPassword key not found");
              }
  
              this.AdminPassword = adminPasswordSetting.Value;
  
              if (String.IsNullOrWhiteSpace(this.AdminPassword))
              {
                  _logger.LogError("AdminPassword not found");
              }
              _initialized = true;
          }
      }
  
      #endregion
  }
  </code></pre>
  
  <p>In the Unity, I'm using the below code:</p>
  
  <pre><code>// IAppConfig
  container.RegisterType&lt;IAppConfig, AppConfig&gt;(new ContainerControlledLifetimeManager(),
                                                new InjectionConstructor(configManager,
                                                                         logger));
  var appConfig = container.Resolve&lt;IAppConfig&gt;();
  appConfig.Initialize();
  </code></pre>