/// <summary> /// Register a singleton with the singleton manager. /// The singleton will be instantiated when InstantiateSingletons is called. /// </summary> public void RegisterSingleton(SingletonDef singletonDef) { Argument.NotNull(() => singletonDef); Argument.NotNull(() => singletonDef.singletonType); singletonDefs.Add(singletonDef); }
/// <summary> /// Instantiate a singleton from a definition. /// </summary> private object InstantiateSingleton(SingletonDef singletonDef, IFactory factory) { var type = singletonDef.singletonType; logger.LogVerbose("Instantiating singleton " + type.Name + " that satisfies dependencies " + singletonDef.dependencyNames.Join(", ")); try { // Instantiate the singleton. var singleton = singletonDef.Instantiate != null?singletonDef.Instantiate(factory, type) : factory.Create(type); singletonDef.dependencyNames.Each(dependencyName => dependencyCache.Add(dependencyName, singleton)); return(singleton); } catch (Exception ex) { logger.LogError(ex, "Failed to instantiate singleton {SingletonTypeName}", type.Name); return(null); } }
/// <summary> /// Instantiate a singleton from a definition. /// </summary> private object InstantiateSingleton(SingletonDef singletonDef, IFactory factory) { var type = singletonDef.singletonType; logger.LogInfo("Instantiating singleton " + type.Name + " that satisfies dependencies " + singletonDef.dependencyNames.Join(", ")); try { // Instantiate the singleton. var singleton = singletonDef.Instantiate != null ? singletonDef.Instantiate(factory, type) : factory.Create(type); singletonDef.dependencyNames.Each(dependencyName => dependencyCache.Add(dependencyName, singleton)); return singleton; } catch (Exception ex) { logger.LogError(ex, "Failed to instantiate singleton {SingletonTypeName}", type.Name); return null; } }