Singleton Pattern in C# - Provider Model - Community Server and DotNetNuke

An interesting discussion of 5 ways to implement the singleton pattern in C# can be read here.

“The singleton pattern is one of the best-known patterns in software engineering. Essentially, a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance. Most commonly, singletons don't allow any parameters to be specified when creating the instance - as otherwise a second request for an instance but with a different parameter could be problematic! (If the same instance should be accessed for all requests with the same parameter, the factory pattern is more appropriate.) This article deals only with the situation where no parameters are required. Typically a requirement of singletons is that they are created lazily - i.e. that the instance isn't created until it is first needed.

There are various different ways of implementing the singleton pattern in C#. I shall present them here in reverse order of elegance, starting with the most commonly seen, which is not thread-safe, and working up to a fully lazily-loaded, thread-safe, simple and highly performant version. Note that in the code here, I omit the private modifier, as it is the default for class members. In many other languages such as Java, there is a different default, and private should be used.

All these implementations share four common characteristics, however:

  • A single constructor, which is private and parameterless. This prevents other classes from instantiating it (which would be a violation of the pattern). Note that it also prevents subclassing - if a singleton can be subclassed once, it can be subclassed twice, and if each of those subclasses can create an instance, the pattern is violated. The factory pattern can be used if you need a single instance of a base type, but the exact type isn't known until runtime.
  • The class is sealed. This is unnecessary, strictly speaking, due to the above point, but may help the JIT to optimise things more.
  • A static variable which holds a reference to the single created instance, if any.
  • A public static means of getting the reference to the single created instance, creating one if necessary.

Note that all of these implementations also use a public static property Instance as the means of accessing the instance. In all cases, the property could easily be converted to a method, with no impact on thread-safety or performance. [Read more]”.

A good example of the singleton pattern in C# can be found in the Community Server Source Code.  Look at the code that implements the Provider Model.  It uses a method as opposed to a property to access Instance.  You can easily convert that to VB.NET, or you can go on to look at the DotNetNuke Architecture and look at how it uses a singleton in its implementation of the Provider Model.

Here is an example of a singleton for use with the Provider Model:

 

Singleton in C# For Provider Model
public abstract class CommonDataProvider
{
    private static readonly string
        CommonDataProviderName = "CommonDataProvider";

    private static CommonDataProvider
        _defaultInstance = null;

    static CommonDataProvider()
    {
        CreateDefaultProvider();
    }

    public static CommonDataProvider Instance() 
    {
        return _defaultInstance;
    }

    private static void CreateDefaultProvider()
    {
        SiteSettings settings = SiteSettings.Fetch();

        Provider commonDataProvider =
            settings.DataProviders[CommonDataProviderName]
            as Provider;

        _defaultInstance =
            DataProviders.CreateInstance(commonDataProvider)
            as CommonDataProvider;
    }
    
    // Abstract Methods...
}

 

posted on Wednesday, April 27, 2005 9:06 PM

My Links

Post Categories

Article Categories

Archives

Loose-Leaf Tea