Click or drag to resize

The NAG Constructor Pattern

This topic contains an article describing a design pattern first introduced in AsyncInit.

This topic contains the following sections:

The Problem

A base class may occasionally require subclasses to limit the visibility of their constructor(s). Common example:

C#
abstract class Singleton<T>
    where T : Singleton<T>
{
    public static readonly T Instance;

    static Singleton()
    {
        Instance = new System.Lazy<T>(CreateInstance).Value;
    }

    private static T CreateInstance()
    {
        return (T)System.Activator.CreateInstance(typeof(T), true);
    }
}

The true bit instructs Activator to look for a non-public constructor, but not to require it. This means that a subclass could easily forgo the private constructor (by e.g. declaring no constructor at all), which would defeat the whole purpose of it trying to be a singleton.

Ideally, we would like to enforce this at compilation time. C# affords us the new constraint:

C#
abstract class NotSingleton<T>
    where T : NotSingleton<T>, new()
{
    //...
}
but not its opposite. Something along the lines of
C#
abstract class Singleton<T>
    where T : Singleton<T>, ~new()
{
    //...
}
would be great, but currently results in a compilation error for the wrong class.

The Solution

AsyncInitBase<T>, a class not too dissimilar from the above Singleton<T>, contains the following code:

C#
/// <summary>
/// Deriving types should define a private parameterless constructor.
/// </summary>
/// <param name="dummy">Dummy parameter (safe to pass <c>null</c>).</param>
protected AsyncInitBase(object dummy)
{
}

Its goal is to enforce implementors to define a constructor of their own, with the hope that it indeed be a private one.

The Name
Null Argument Guidance Constructor, or NAG Constructor, was the name originally coined for this concept. We believe it best encompasses both the means and (in its acronym form) the end.
See Also