-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename SingletonBaseClass to SingletonBase
- Loading branch information
1 parent
e4a4ff2
commit b6e03da
Showing
12 changed files
with
61 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
[](https://www.nuget.org/packages/TJC.Singleton) | ||
|
||
# TJC Singleton | ||
The singleton base class ensures that *only* one **instance** of a class will exist. | ||
This singleton base & factory can be used to instantiate all singletons at once. | ||
|
||
## Details | ||
- This is done in a thread-safe way using a **Lazy** implementation. | ||
--- | ||
## [SingletonBase](./TJC.Singleton/SingletonBase.cs) | ||
- Ensures that *only* one **instance** of a **derived class** will exist. | ||
- This is implemented in a thread-safe way using **Lazy**. | ||
- It also ensures that the derived class has a **private** or **protected** constructor by throwing an **exception** at run-time. | ||
|
||
## Examples | ||
- Please see [this mock class](./TJC.Singleton.Tests/Mocks/Valid/MockSingletonValid.cs) for an example of the intended use of the [SingletonBaseClass](./TJC.Singleton/SingletonBaseClass.cs). | ||
### Examples | ||
- [This mock class](./TJC.Singleton.Tests/Mocks/Valid/MockSingletonValid.cs) shows a example use-case. | ||
|
||
--- | ||
## [SingletonFactory](./TJC.Singleton/Factories/SingletonFactory.cs) | ||
- Can be used to instantiate **ALL** classes that derive from [SingletonBase](./TJC.Singleton/SingletonBase.cs). |
2 changes: 1 addition & 1 deletion
2
TJC.Singleton.Tests/Mocks/InvalidConstructors/MockSingletonNoConstructor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace TJC.Singleton.Tests.Mocks.InvalidConstructors; | ||
|
||
internal class MockSingletonNoConstructor : SingletonBaseClass<MockSingletonNoConstructor>, IIdentifier | ||
internal class MockSingletonNoConstructor : SingletonBase<MockSingletonNoConstructor>, IIdentifier | ||
{ | ||
public Guid Id { get; } = Guid.NewGuid(); | ||
} |
2 changes: 1 addition & 1 deletion
2
...ingleton.Tests/Mocks/InvalidConstructors/MockSingletonPrivateConstructorWithParameters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...gleton.Tests/Mocks/InvalidConstructors/MockSingletonProtectedConstructorWithParameters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
TJC.Singleton.Tests/Mocks/InvalidConstructors/MockSingletonPublicParameterlessConstructor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 37 additions & 37 deletions
74
TJC.Singleton/SingletonBaseClass.cs → TJC.Singleton/SingletonBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
using TJC.Singleton.Exceptions; | ||
using TJC.Singleton.Helpers; | ||
|
||
namespace TJC.Singleton; | ||
|
||
/// <summary> | ||
/// Creates a single instance of <seealso cref="TDerivedClass"/> that can be accessed through the <see cref="Instance"/> property. | ||
/// </summary> | ||
/// <typeparam name="TDerivedClass"></typeparam> | ||
/// <exception cref="InvalidSingletonConstructorException">Must have a non-public parameterless constructor.</exception> | ||
public abstract class SingletonBaseClass<TDerivedClass> where TDerivedClass : SingletonBaseClass<TDerivedClass> | ||
{ | ||
#region Fields | ||
|
||
private static readonly Lazy<TDerivedClass> _instance = new(CreateInstance); | ||
|
||
#endregion | ||
|
||
#region Properties | ||
|
||
public static TDerivedClass Instance => _instance.Value; | ||
|
||
public static bool IsInstantiated => _instance.IsValueCreated; | ||
|
||
#endregion | ||
|
||
#region Methods | ||
|
||
private static TDerivedClass CreateInstance() | ||
{ | ||
// Use reflection to create an instance of the derived class. | ||
var ctor = SingletonConstructorHelpers.GetSingletonConstructor<TDerivedClass>(); | ||
return (TDerivedClass)ctor.Invoke(null) ?? | ||
throw new SingletonInitializationException($"[{typeof(TDerivedClass)}] singleton failed to initialize"); | ||
} | ||
|
||
#endregion | ||
using TJC.Singleton.Exceptions; | ||
using TJC.Singleton.Helpers; | ||
|
||
namespace TJC.Singleton; | ||
|
||
/// <summary> | ||
/// Creates a single instance of <seealso cref="TDerivedClass"/> that can be accessed through the <see cref="Instance"/> property. | ||
/// </summary> | ||
/// <typeparam name="TDerivedClass"></typeparam> | ||
/// <exception cref="InvalidSingletonConstructorException">Must have a non-public parameterless constructor.</exception> | ||
public abstract class SingletonBase<TDerivedClass> where TDerivedClass : SingletonBase<TDerivedClass> | ||
{ | ||
#region Fields | ||
|
||
private static readonly Lazy<TDerivedClass> _instance = new(CreateInstance); | ||
|
||
#endregion | ||
|
||
#region Properties | ||
|
||
public static TDerivedClass Instance => _instance.Value; | ||
|
||
public static bool IsInstantiated => _instance.IsValueCreated; | ||
|
||
#endregion | ||
|
||
#region Methods | ||
|
||
private static TDerivedClass CreateInstance() | ||
{ | ||
// Use reflection to create an instance of the derived class. | ||
var ctor = SingletonConstructorHelpers.GetSingletonConstructor<TDerivedClass>(); | ||
return (TDerivedClass)ctor.Invoke(null) ?? | ||
throw new SingletonInitializationException($"[{typeof(TDerivedClass)}] singleton failed to initialize"); | ||
} | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters