From 88ab13a82da23178c9cbfd5ab6a1cb266f26273e Mon Sep 17 00:00:00 2001 From: Tyler Carrol Date: Sun, 20 Jul 2025 14:25:59 -0400 Subject: [PATCH 1/3] Update comments --- TJC.Singleton/Factories/SingletonFactory.cs | 3 +++ TJC.Singleton/Helpers/SingletonConstructorHelpers.cs | 7 +++++-- TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs | 3 +++ TJC.Singleton/SingletonBase.cs | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/TJC.Singleton/Factories/SingletonFactory.cs b/TJC.Singleton/Factories/SingletonFactory.cs index 7364266..b5ec704 100644 --- a/TJC.Singleton/Factories/SingletonFactory.cs +++ b/TJC.Singleton/Factories/SingletonFactory.cs @@ -4,6 +4,9 @@ namespace TJC.Singleton.Factories; +/// +/// Factory for creating 's. +/// public static class SingletonFactory { #region Constants diff --git a/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs b/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs index f32fd24..843f3c2 100644 --- a/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs +++ b/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs @@ -3,12 +3,15 @@ namespace TJC.Singleton.Helpers; +/// +/// Helpers for constructing . +/// public static class SingletonConstructorHelpers { #region Get Singlet Constructor /// - /// Gets the singleton constructor for the type . + /// Gets the singleton constructor for the type . /// /// /// @@ -45,7 +48,7 @@ public static ConstructorInfo GetSingletonConstructor(Type type) #region Check if Singleton has Valid Constructor /// - /// Checks if a singleton of type has a valid constructor. + /// Checks if a singleton of type . has a valid constructor. /// /// /// diff --git a/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs b/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs index 136e2ba..9961391 100644 --- a/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs +++ b/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs @@ -1,5 +1,8 @@ namespace TJC.Singleton.Helpers; +/// +/// Helpers for identifying details about . +/// public static class SingletonIdentifierHelpers { /// diff --git a/TJC.Singleton/SingletonBase.cs b/TJC.Singleton/SingletonBase.cs index 810bcae..35016e1 100644 --- a/TJC.Singleton/SingletonBase.cs +++ b/TJC.Singleton/SingletonBase.cs @@ -4,7 +4,7 @@ namespace TJC.Singleton; /// -/// Creates a single instance of that can be accessed through the property. +/// Creates a single instance of that can be accessed through the property. /// /// /// Must have a non-public parameterless constructor. From ba3ebfa812963eedad4e0877d52da1d8c2c7fb4a Mon Sep 17 00:00:00 2001 From: Tyler Carrol Date: Sun, 20 Jul 2025 14:28:28 -0400 Subject: [PATCH 2/3] Replace trace with ILogger --- .../Mocks/Logging/MockLogger.cs | 19 ++++++++++ .../Tests/Instantiated/IsInstantiatedTest.cs | 3 +- TJC.Singleton/Factories/SingletonFactory.cs | 37 +++++++++---------- TJC.Singleton/TJC.Singleton.csproj | 6 +-- 4 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs diff --git a/TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs b/TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs new file mode 100644 index 0000000..004abce --- /dev/null +++ b/TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.Logging; + +namespace TJC.Singleton.Tests.Mocks.Logging; + +internal class MockLogger : ILogger +{ + public static MockLogger Default => new(); + + public IDisposable? BeginScope(TState state) + where TState : notnull + => default!; + + public bool IsEnabled(LogLevel logLevel) => true; + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + { + Trace.WriteLine($"[{logLevel}:{eventId}] {formatter(state, exception)}"); + } +} diff --git a/TJC.Singleton.Tests/Tests/Instantiated/IsInstantiatedTest.cs b/TJC.Singleton.Tests/Tests/Instantiated/IsInstantiatedTest.cs index 4af25d0..7fd86bc 100644 --- a/TJC.Singleton.Tests/Tests/Instantiated/IsInstantiatedTest.cs +++ b/TJC.Singleton.Tests/Tests/Instantiated/IsInstantiatedTest.cs @@ -1,4 +1,5 @@ using TJC.Singleton.Factories; +using TJC.Singleton.Tests.Mocks.Logging; namespace TJC.Singleton.Tests.Tests.Instantiated; @@ -13,7 +14,7 @@ public void SingletonGetInstantiatedAfterBeingReferencedTest() MockSingletonInstantiated.IsInstantiated, $"{nameof(MockSingletonInstantiated)} was already instantiated" ); - SingletonFactory.InstantiateAll(trace: true); + SingletonFactory.InstantiateAll(MockLogger.Default); Assert.IsTrue( MockSingletonInstantiated.IsInstantiated, $"{nameof(MockSingletonInstantiated)} is not instantiated" diff --git a/TJC.Singleton/Factories/SingletonFactory.cs b/TJC.Singleton/Factories/SingletonFactory.cs index b5ec704..1920ac2 100644 --- a/TJC.Singleton/Factories/SingletonFactory.cs +++ b/TJC.Singleton/Factories/SingletonFactory.cs @@ -1,4 +1,4 @@ -using System.Diagnostics; +using Microsoft.Extensions.Logging; using System.Reflection; using TJC.Singleton.Helpers; @@ -20,19 +20,19 @@ private class PlaceholderSingleton : SingletonBase; /// /// Instantiate all singletons in the current app domain. /// - /// /// + /// + /// /// - public static void InstantiateAll(bool trace = true, bool throwIfFailed = false) + public static void InstantiateAll(ILogger? logger = null, LogLevel logLevel = LogLevel.Trace, bool throwIfFailed = false) { var failedToInstantiate = new List(); var singletons = GetSingletonTypes(); - - if (trace) - Trace.WriteLine($"{singletons.Count} Singletons Found"); + + logger?.Log(logLevel, "{count} Singletons Found", singletons.Count); foreach (var singleton in singletons) - if (!singleton.Instantiate(trace)) + if (!singleton.Instantiate(logger, logLevel)) failedToInstantiate.Add(singleton.Name); if (throwIfFailed && failedToInstantiate.Count > 0) @@ -66,34 +66,33 @@ public static List GetSingletonTypes() } /// - /// Instantiates a singleton of type . + /// Instantiates a singleton of type . /// /// - /// + /// + /// /// - public static bool Instantiate(bool trace) => Instantiate(typeof(T), trace); + public static bool Instantiate(ILogger? logger = null, LogLevel logLevel = LogLevel.Trace) => + Instantiate(typeof(T), logger, logLevel); /// /// Instantiates a singleton of given type. /// /// - /// + /// + /// /// /// - private static bool Instantiate(this Type singleton, bool trace) - { - if (trace) - Trace.WriteLine($"[{singleton.Name}] Instantiating"); + private static bool Instantiate(this Type singleton, ILogger? logger = null, LogLevel logLevel = LogLevel.Trace) + { + logger?.Log(logLevel, "[{name}] Instantiating", singleton.Name); var instanceProp = singleton.GetProperty( InstanceName, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy ) ?? throw new Exception($"[{singleton.Name}] does not have property [{InstanceName}]"); var instanceValue = instanceProp.GetValue(singleton); - if (trace) - Trace.WriteLine( - $"[{singleton.Name}] {(instanceValue != null ? "Instantiated" : "Failed to Instantiate")}" - ); + logger?.Log(logLevel, "[{name}] {result}", singleton.Name, instanceValue != null ? "Instantiated" : "Failed to Instantiate"); return instanceValue != null; } } diff --git a/TJC.Singleton/TJC.Singleton.csproj b/TJC.Singleton/TJC.Singleton.csproj index 9db1292..6cfc508 100644 --- a/TJC.Singleton/TJC.Singleton.csproj +++ b/TJC.Singleton/TJC.Singleton.csproj @@ -34,15 +34,15 @@ - - - <_Parameter1>TJC.Singleton.Tests + + + From 383f82f1d5eb9b1a336ade615c5cdcc1135db681 Mon Sep 17 00:00:00 2001 From: TylerCarrol <25536704+TylerCarrol@users.noreply.github.com> Date: Sun, 20 Jul 2025 18:32:30 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=A7=B9=20chore:=20[MegaLinter]=20Appl?= =?UTF-8?q?y=20[1]=20automatic=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Mocks/Logging/MockLogger.cs | 11 +++-- TJC.Singleton/Factories/SingletonFactory.cs | 33 +++++++++---- .../Helpers/SingletonConstructorHelpers.cs | 4 +- .../Helpers/SingletonIdentifierHelpers.cs | 4 +- TJC.Singleton/TJC.Singleton.csproj | 47 ++++++++++++------- 5 files changed, 65 insertions(+), 34 deletions(-) diff --git a/TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs b/TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs index 004abce..ccea8d8 100644 --- a/TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs +++ b/TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs @@ -7,12 +7,17 @@ internal class MockLogger : ILogger public static MockLogger Default => new(); public IDisposable? BeginScope(TState state) - where TState : notnull - => default!; + where TState : notnull => default!; public bool IsEnabled(LogLevel logLevel) => true; - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + public void Log( + LogLevel logLevel, + EventId eventId, + TState state, + Exception? exception, + Func formatter + ) { Trace.WriteLine($"[{logLevel}:{eventId}] {formatter(state, exception)}"); } diff --git a/TJC.Singleton/Factories/SingletonFactory.cs b/TJC.Singleton/Factories/SingletonFactory.cs index 1920ac2..d6e6875 100644 --- a/TJC.Singleton/Factories/SingletonFactory.cs +++ b/TJC.Singleton/Factories/SingletonFactory.cs @@ -1,11 +1,11 @@ -using Microsoft.Extensions.Logging; -using System.Reflection; +using System.Reflection; +using Microsoft.Extensions.Logging; using TJC.Singleton.Helpers; namespace TJC.Singleton.Factories; -/// -/// Factory for creating 's. +/// +/// Factory for creating 's. /// public static class SingletonFactory { @@ -24,11 +24,15 @@ private class PlaceholderSingleton : SingletonBase; /// /// /// - public static void InstantiateAll(ILogger? logger = null, LogLevel logLevel = LogLevel.Trace, bool throwIfFailed = false) + public static void InstantiateAll( + ILogger? logger = null, + LogLevel logLevel = LogLevel.Trace, + bool throwIfFailed = false + ) { var failedToInstantiate = new List(); var singletons = GetSingletonTypes(); - + logger?.Log(logLevel, "{count} Singletons Found", singletons.Count); foreach (var singleton in singletons) @@ -72,7 +76,7 @@ public static List GetSingletonTypes() /// /// /// - public static bool Instantiate(ILogger? logger = null, LogLevel logLevel = LogLevel.Trace) => + public static bool Instantiate(ILogger? logger = null, LogLevel logLevel = LogLevel.Trace) => Instantiate(typeof(T), logger, logLevel); /// @@ -83,8 +87,12 @@ public static bool Instantiate(ILogger? logger = null, LogLevel logLevel = Lo /// /// /// - private static bool Instantiate(this Type singleton, ILogger? logger = null, LogLevel logLevel = LogLevel.Trace) - { + private static bool Instantiate( + this Type singleton, + ILogger? logger = null, + LogLevel logLevel = LogLevel.Trace + ) + { logger?.Log(logLevel, "[{name}] Instantiating", singleton.Name); var instanceProp = singleton.GetProperty( @@ -92,7 +100,12 @@ private static bool Instantiate(this Type singleton, ILogger? logger = null, Log BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy ) ?? throw new Exception($"[{singleton.Name}] does not have property [{InstanceName}]"); var instanceValue = instanceProp.GetValue(singleton); - logger?.Log(logLevel, "[{name}] {result}", singleton.Name, instanceValue != null ? "Instantiated" : "Failed to Instantiate"); + logger?.Log( + logLevel, + "[{name}] {result}", + singleton.Name, + instanceValue != null ? "Instantiated" : "Failed to Instantiate" + ); return instanceValue != null; } } diff --git a/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs b/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs index 843f3c2..7cff52c 100644 --- a/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs +++ b/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs @@ -3,8 +3,8 @@ namespace TJC.Singleton.Helpers; -/// -/// Helpers for constructing . +/// +/// Helpers for constructing . /// public static class SingletonConstructorHelpers { diff --git a/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs b/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs index 9961391..6b938e5 100644 --- a/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs +++ b/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs @@ -1,7 +1,7 @@ namespace TJC.Singleton.Helpers; -/// -/// Helpers for identifying details about . +/// +/// Helpers for identifying details about . /// public static class SingletonIdentifierHelpers { diff --git a/TJC.Singleton/TJC.Singleton.csproj b/TJC.Singleton/TJC.Singleton.csproj index 6cfc508..455c350 100644 --- a/TJC.Singleton/TJC.Singleton.csproj +++ b/TJC.Singleton/TJC.Singleton.csproj @@ -8,11 +8,11 @@ Singleton Base & Singleton Factory Tyler Carrol README.md - LICENSE + LICENSE $([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../CHANGELOG.md")) https://github.com/TJC-Tools/TJC.Singleton true - + @@ -31,7 +31,7 @@ True \ - + @@ -44,7 +44,11 @@ - + @@ -52,13 +56,17 @@ - - + @@ -66,18 +74,18 @@ - - + @@ -99,6 +107,11 @@ - +