Class GenericSingletonBase<T>
Abstract class GenericSingletonBase is a complete implementation of the Singleton design pattern that takes full advantage of the Microsoft .NET Framework. Please see the Remarks for further details.
Inheritance
Namespace: WizardWrx
Assembly: WizardWrx.Core.dll
Syntax
public abstract class GenericSingletonBase<T> : object where T : GenericSingletonBase<T>
Type Parameters
Name | Description |
---|---|
T | This class uses a recursive constraint on T, to require it to be derived from this base class. |
Remarks
The optimizations in this implementation take advantage of a guarantee made by the framework that it won't bother to call a static constructor on a class until its first use. Moreover, a static constructor is never called more than once, no matter how many subsequent references to the class occur.
Taking advantage of these features of the framework eliminates the need for synchronization, and replaces a method call with a direct reference to the static read only property that returns a reference to the one and only instance.
Constructors
| Improve this Doc View SourceGenericSingletonBase()
The private constructor has no real work to do, but it must exist to prevent the framework from generating a public constructor, which would violate a critical constraint of the Singleton design pattern.
Declaration
protected GenericSingletonBase()
Remarks
This property is marked protected to give derived classes direct access to it, which their static members require. For example, in ExceptionLogger, the static initializer has more work to do that it cannot start until an instance exists.
Fields
| Improve this Doc View Sources_genTheOnlyInstance
This static member holds the reference to the one and only instance of the derived class that is permitted.
Declaration
protected static T s_genTheOnlyInstance
Field Value
Type | Description |
---|---|
T |
Remarks
This property is marked protected to give derived classes direct access to it, which their static members require. For example, in ExceptionLogger, the static initializer has more work to do that it cannot start until an instance exists.
Properties
| Improve this Doc View SourceTheOnlyInstance
This implementation simplifies access to the single instance by way of this static read-only property that returns the reference to the instance stored in its one and only private static member.
Declaration
public static T TheOnlyInstance { get; }
Property Value
Type | Description |
---|---|
T |
Remarks
The sweet thing about this implementation is that your code doesn't need a copy of the reference, since a tail call on the static property is sufficient.
Methods
| Improve this Doc View SourceGetTheSingleInstance()
Implement the traditional GetTheSingleInstance method for obtaining a reference to a Singleton object.
Declaration
public static T GetTheSingleInstance()
Returns
Type | Description |
---|---|
T | This method should always succeed by returning a reference to the one and only instance of the derived class. |