Singleton and Ingo's RemotingHelper

I have been using Ingo Rammer's RemotingHelper class for a while now. I'm not a big fan of a lot of statics in my apps so I rewrote it to a Singleton class based on microsofts suggestion for C#. The problem with this was that the constructor for the RemotingHelper class were run before RemotingConfiguration.Configure()which naturally caused problems.

As a consequence I rewrote the Singleton to do lazy initialization like this:

public sealed class ServiceLocator
{
      
private static ServiceLocator instance;
      
public static ServiceLocator Instance
       {
              
get
              
{
                 
if(instance == null)
                          instance =
new ServiceLocator();
                  
return instance;
               }
        }

        private ServiceLocator() {}
}

Are there any reason that this will cause any problems? This could maybe be an alternative strategy in the C# pattern?

3 Comments

  • class ServiceLocator


    {


    public static ServiceLocatorInstance() {


    if (_instance == null) {


    lock (typeof(ServiceLocator)) {


    if (_instance == null) {


    _instance = new ServiceLocator();


    }


    }


    }


    return _instance;


    }


    protected ServiceLocator() {}


    private static volatile ServiceLocator _instance = null;


    }


    As per the microsoft.com article "Exploring the Singleton Design Pattern".

  • If you read the last sample in the article, Microsoft says that their simplest implementation handles the functionality that the preceeding sample (the one you pasted) tries to accomplish. My experience is that it doesn't. <br><br> The one you describe might be a better solution than the one I use though because of the lock.

  • Hi - would you have a complete code listing available ? cheers!

Comments have been disabled for this content.