1- using Microsoft . Extensions . Logging ;
2- using System . Reflection ;
1+ using System . Reflection ;
2+ using Microsoft . Extensions . Logging ;
33using TJC . Singleton . Helpers ;
44
55namespace TJC . Singleton . Factories ;
66
7- /// <summary>
8- /// Factory for creating <seealso cref="SingletonBase{TDerivedClass}"/>'s.
7+ /// <summary>
8+ /// Factory for creating <seealso cref="SingletonBase{TDerivedClass}"/>'s.
99/// </summary>
1010public static class SingletonFactory
1111{
@@ -24,11 +24,15 @@ private class PlaceholderSingleton : SingletonBase<PlaceholderSingleton>;
2424 /// <param name="logger"></param>
2525 /// <param name="logLevel"></param>
2626 /// <exception cref="Exception"></exception>
27- public static void InstantiateAll ( ILogger ? logger = null , LogLevel logLevel = LogLevel . Trace , bool throwIfFailed = false )
27+ public static void InstantiateAll (
28+ ILogger ? logger = null ,
29+ LogLevel logLevel = LogLevel . Trace ,
30+ bool throwIfFailed = false
31+ )
2832 {
2933 var failedToInstantiate = new List < string > ( ) ;
3034 var singletons = GetSingletonTypes ( ) ;
31-
35+
3236 logger ? . Log ( logLevel , "{count} Singletons Found" , singletons . Count ) ;
3337
3438 foreach ( var singleton in singletons )
@@ -72,7 +76,7 @@ public static List<Type> GetSingletonTypes()
7276 /// <param name="logger"></param>
7377 /// <param name="logLevel"></param>
7478 /// <returns></returns>
75- public static bool Instantiate < T > ( ILogger ? logger = null , LogLevel logLevel = LogLevel . Trace ) =>
79+ public static bool Instantiate < T > ( ILogger ? logger = null , LogLevel logLevel = LogLevel . Trace ) =>
7680 Instantiate ( typeof ( T ) , logger , logLevel ) ;
7781
7882 /// <summary>
@@ -83,16 +87,25 @@ public static bool Instantiate<T>(ILogger? logger = null, LogLevel logLevel = Lo
8387 /// <param name="logLevel"></param>
8488 /// <returns></returns>
8589 /// <exception cref="Exception"></exception>
86- private static bool Instantiate ( this Type singleton , ILogger ? logger = null , LogLevel logLevel = LogLevel . Trace )
87- {
90+ private static bool Instantiate (
91+ this Type singleton ,
92+ ILogger ? logger = null ,
93+ LogLevel logLevel = LogLevel . Trace
94+ )
95+ {
8896 logger ? . Log ( logLevel , "[{name}] Instantiating" , singleton . Name ) ;
8997 var instanceProp =
9098 singleton . GetProperty (
9199 InstanceName ,
92100 BindingFlags . Public | BindingFlags . Static | BindingFlags . FlattenHierarchy
93101 ) ?? throw new Exception ( $ "[{ singleton . Name } ] does not have property [{ InstanceName } ]") ;
94102 var instanceValue = instanceProp . GetValue ( singleton ) ;
95- logger ? . Log ( logLevel , "[{name}] {result}" , singleton . Name , instanceValue != null ? "Instantiated" : "Failed to Instantiate" ) ;
103+ logger ? . Log (
104+ logLevel ,
105+ "[{name}] {result}" ,
106+ singleton . Name ,
107+ instanceValue != null ? "Instantiated" : "Failed to Instantiate"
108+ ) ;
96109 return instanceValue != null ;
97110 }
98111}
0 commit comments