Get rid of container TryGetInstance

This commit is contained in:
Stephan
2018-08-27 18:08:59 +02:00
parent e8dfca5635
commit 00b1dda2fe
5 changed files with 15 additions and 38 deletions

View File

@@ -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()

View File

@@ -19,17 +19,6 @@ namespace Umbraco.Core.Composing
public static T GetInstance<T>(this IContainer container)
=> (T) container.GetInstance(typeof(T));
/// <summary>
/// Tries to get an instance.
/// </summary>
/// <typeparam name="T">The type of the instance.</typeparam>
/// <returns>An instance of the specified type, or null.</returns>
/// <remarks>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.</remarks>
public static T TryGetInstance<T>(this IContainer container)
=> (T) container.TryGetInstance(typeof(T));
/// <summary>
/// Gets registration for a service.
/// </summary>

View File

@@ -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<T>(IContainer container)
where T : class
{
var registration = container?.GetRegistered<T>();
return registration == null ? null : container.GetInstance<T>();
}
public static IShortStringHelper ShortStringHelper
=> _shortStringHelper ?? (_shortStringHelper = _container?.TryGetInstance<IShortStringHelper>()
=> _shortStringHelper ?? (_shortStringHelper = TryGetInstance<IShortStringHelper>(_container)
?? new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(UmbracoConfig.For.UmbracoSettings())));
public static ILogger Logger
=> _logger ?? (_logger = _container?.TryGetInstance<ILogger>()
=> _logger ?? (_logger = TryGetInstance<ILogger>(_container)
?? new DebugDiagnosticsLogger());
public static IProfiler Profiler
=> _profiler ?? (_profiler = _container?.TryGetInstance<IProfiler>()
=> _profiler ?? (_profiler = TryGetInstance<IProfiler>(_container)
?? new LogProfiler(Logger));
public static ProfilingLogger ProfilingLogger
=> _profilingLogger ?? (_profilingLogger = _container?.TryGetInstance<ProfilingLogger>())
=> _profilingLogger ?? (_profilingLogger = TryGetInstance<ProfilingLogger>(_container))
?? new ProfilingLogger(Logger, Profiler);
public static IRuntimeState RuntimeState

View File

@@ -37,16 +37,6 @@ namespace Umbraco.Core.Composing
/// <remarks>Throws an exception if the container failed to get an instance of the specified type.</remarks>
object GetInstance(Type type, string name);
/// <summary>
/// Tries to get an instance.
/// </summary>
/// <param name="type">The type of the instance.</param>
/// <returns>An instance of the specified type, or null.</returns>
/// <remarks>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.</remarks>
object TryGetInstance(Type type);
/// <summary>
/// Gets all instances of a service.
/// </summary>

View File

@@ -107,10 +107,6 @@ namespace Umbraco.Core.Composing.LightInject
public object GetInstance(Type type, string name)
=> Container.GetInstance(type, name);
/// <inheritdoc />
public object TryGetInstance(Type type)
=> Container.TryGetInstance(type);
/// <inheritdoc />
public IEnumerable<T> GetAllInstances<T>()
=> Container.GetAllInstances<T>();