From 00b1dda2fe86b6902fa54a0fbda5fb8622691435 Mon Sep 17 00:00:00 2001 From: Stephan Date: Mon, 27 Aug 2018 18:08:59 +0200 Subject: [PATCH] Get rid of container TryGetInstance --- 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, 15 insertions(+), 38 deletions(-) diff --git a/src/Umbraco.Core/Components/BootLoader.cs b/src/Umbraco.Core/Components/BootLoader.cs index 70e62c799f..fd09ce839d 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)) + .Select(x => GetParameter(componentType, x.ParameterType, x.Name)) .ToArray(); initializer.Invoke(component, parameters); } @@ -328,21 +328,16 @@ namespace Umbraco.Core.Components } } - private object GetParameter(Type componentType, Type parameterType) + private object GetParameter(Type componentType, Type parameterType, string parameterName) { - object param; - try { - param = _container.TryGetInstance(parameterType); + return _container.GetInstance(parameterType); } catch (Exception e) { - throw new BootFailedException($"Could not get parameter of type {parameterType.FullName} for component {componentType.FullName}.", e); + throw new BootFailedException($"Could not get parameter '{parameterName}' 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 423af98691..c01dbe911e 100644 --- a/src/Umbraco.Core/Composing/ContainerExtensions.cs +++ b/src/Umbraco.Core/Composing/ContainerExtensions.cs @@ -19,17 +19,6 @@ 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 d0025c0c8b..f2c694658f 100644 --- a/src/Umbraco.Core/Composing/Current.cs +++ b/src/Umbraco.Core/Composing/Current.cs @@ -78,20 +78,27 @@ 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 = _container?.TryGetInstance() + => _shortStringHelper ?? (_shortStringHelper = TryGetInstance(_container) ?? new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(UmbracoConfig.For.UmbracoSettings()))); public static ILogger Logger - => _logger ?? (_logger = _container?.TryGetInstance() + => _logger ?? (_logger = TryGetInstance(_container) ?? new DebugDiagnosticsLogger()); public static IProfiler Profiler - => _profiler ?? (_profiler = _container?.TryGetInstance() + => _profiler ?? (_profiler = TryGetInstance(_container) ?? new LogProfiler(Logger)); public static ProfilingLogger ProfilingLogger - => _profilingLogger ?? (_profilingLogger = _container?.TryGetInstance()) + => _profilingLogger ?? (_profilingLogger = TryGetInstance(_container)) ?? 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 dab5dd159c..1ec1dbe44f 100644 --- a/src/Umbraco.Core/Composing/IContainer.cs +++ b/src/Umbraco.Core/Composing/IContainer.cs @@ -37,16 +37,6 @@ 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 0a81f0e9eb..502dc91ff8 100644 --- a/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs +++ b/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs @@ -107,10 +107,6 @@ 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();