From fed9e2733211af37baff1572e52f3a3d30fd71d9 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 29 Aug 2018 18:50:08 +0200 Subject: [PATCH] Revert "Get rid of container TryGetInstance" This reverts commit 00b1dda2fe86b6902fa54a0fbda5fb8622691435. --- src/Umbraco.Core/Components/BootLoader.cs | 13 +++++++++---- src/Umbraco.Core/Composing/ContainerExtensions.cs | 11 +++++++++++ src/Umbraco.Core/Composing/Current.cs | 15 ++++----------- src/Umbraco.Core/Composing/IContainer.cs | 10 ++++++++++ .../Composing/LightInject/LightInjectContainer.cs | 4 ++++ 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/Umbraco.Core/Components/BootLoader.cs b/src/Umbraco.Core/Components/BootLoader.cs index 87d4cdac75..e28b6a38b8 100644 --- a/src/Umbraco.Core/Components/BootLoader.cs +++ b/src/Umbraco.Core/Components/BootLoader.cs @@ -319,7 +319,7 @@ namespace Umbraco.Core.Components foreach (var initializer in initializers) { var parameters = initializer.GetParameters() - .Select(x => GetParameter(componentType, x.ParameterType, x.Name)) + .Select(x => GetParameter(componentType, x.ParameterType)) .ToArray(); initializer.Invoke(component, parameters); } @@ -328,16 +328,21 @@ namespace Umbraco.Core.Components } } - private object GetParameter(Type componentType, Type parameterType, string parameterName) + private object GetParameter(Type componentType, Type parameterType) { + object param; + try { - return _container.GetInstance(parameterType); + param = _container.TryGetInstance(parameterType); } catch (Exception e) { - throw new BootFailedException($"Could not get parameter '{parameterName}' of type {parameterType.FullName} for component {componentType.FullName}.", e); + throw new BootFailedException($"Could not get parameter of type {parameterType.FullName} for component {componentType.FullName}.", e); } + + if (param == null) throw new BootFailedException($"Could not get parameter of type {parameterType.FullName} for component {componentType.FullName}."); + return param; } public void Terminate() diff --git a/src/Umbraco.Core/Composing/ContainerExtensions.cs b/src/Umbraco.Core/Composing/ContainerExtensions.cs index c01dbe911e..423af98691 100644 --- a/src/Umbraco.Core/Composing/ContainerExtensions.cs +++ b/src/Umbraco.Core/Composing/ContainerExtensions.cs @@ -19,6 +19,17 @@ namespace Umbraco.Core.Composing public static T GetInstance(this IContainer container) => (T) container.GetInstance(typeof(T)); + /// + /// Tries to get an instance. + /// + /// The type of the instance. + /// An instance of the specified type, or null. + /// Returns null if the container does not know how to get an instance + /// of the specified type. Throws an exception if the container does know how + /// to get an instance of the specified type, but failed to do so. + public static T TryGetInstance(this IContainer container) + => (T) container.TryGetInstance(typeof(T)); + /// /// Gets registration for a service. /// diff --git a/src/Umbraco.Core/Composing/Current.cs b/src/Umbraco.Core/Composing/Current.cs index f2c694658f..d0025c0c8b 100644 --- a/src/Umbraco.Core/Composing/Current.cs +++ b/src/Umbraco.Core/Composing/Current.cs @@ -78,27 +78,20 @@ namespace Umbraco.Core.Composing // registered we setup a default one. We should really refactor our tests so that it does // not happen. Will do when we get rid of IShortStringHelper. - private static T TryGetInstance(IContainer container) - where T : class - { - var registration = container?.GetRegistered(); - return registration == null ? null : container.GetInstance(); - } - public static IShortStringHelper ShortStringHelper - => _shortStringHelper ?? (_shortStringHelper = TryGetInstance(_container) + => _shortStringHelper ?? (_shortStringHelper = _container?.TryGetInstance() ?? new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(UmbracoConfig.For.UmbracoSettings()))); public static ILogger Logger - => _logger ?? (_logger = TryGetInstance(_container) + => _logger ?? (_logger = _container?.TryGetInstance() ?? new DebugDiagnosticsLogger()); public static IProfiler Profiler - => _profiler ?? (_profiler = TryGetInstance(_container) + => _profiler ?? (_profiler = _container?.TryGetInstance() ?? new LogProfiler(Logger)); public static ProfilingLogger ProfilingLogger - => _profilingLogger ?? (_profilingLogger = TryGetInstance(_container)) + => _profilingLogger ?? (_profilingLogger = _container?.TryGetInstance()) ?? new ProfilingLogger(Logger, Profiler); public static IRuntimeState RuntimeState diff --git a/src/Umbraco.Core/Composing/IContainer.cs b/src/Umbraco.Core/Composing/IContainer.cs index 1ec1dbe44f..dab5dd159c 100644 --- a/src/Umbraco.Core/Composing/IContainer.cs +++ b/src/Umbraco.Core/Composing/IContainer.cs @@ -37,6 +37,16 @@ namespace Umbraco.Core.Composing /// Throws an exception if the container failed to get an instance of the specified type. object GetInstance(Type type, string name); + /// + /// Tries to get an instance. + /// + /// The type of the instance. + /// An instance of the specified type, or null. + /// Returns null if the container does not know how to get an instance + /// of the specified type. Throws an exception if the container does know how + /// to get an instance of the specified type, but failed to do so. + object TryGetInstance(Type type); + /// /// Gets all instances of a service. /// diff --git a/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs b/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs index b8ba597578..ecb8f0a460 100644 --- a/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs +++ b/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs @@ -109,6 +109,10 @@ namespace Umbraco.Core.Composing.LightInject public object GetInstance(Type type, string name) => Container.GetInstance(type, name); + /// + public object TryGetInstance(Type type) + => Container.TryGetInstance(type); + /// public IEnumerable GetAllInstances() => Container.GetAllInstances();