Revert "Get rid of container TryGetInstance"
This reverts commit 00b1dda2fe.
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -19,6 +19,17 @@ 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>
|
||||
|
||||
@@ -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<T>(IContainer container)
|
||||
where T : class
|
||||
{
|
||||
var registration = container?.GetRegistered<T>();
|
||||
return registration == null ? null : container.GetInstance<T>();
|
||||
}
|
||||
|
||||
public static IShortStringHelper ShortStringHelper
|
||||
=> _shortStringHelper ?? (_shortStringHelper = TryGetInstance<IShortStringHelper>(_container)
|
||||
=> _shortStringHelper ?? (_shortStringHelper = _container?.TryGetInstance<IShortStringHelper>()
|
||||
?? new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(UmbracoConfig.For.UmbracoSettings())));
|
||||
|
||||
public static ILogger Logger
|
||||
=> _logger ?? (_logger = TryGetInstance<ILogger>(_container)
|
||||
=> _logger ?? (_logger = _container?.TryGetInstance<ILogger>()
|
||||
?? new DebugDiagnosticsLogger());
|
||||
|
||||
public static IProfiler Profiler
|
||||
=> _profiler ?? (_profiler = TryGetInstance<IProfiler>(_container)
|
||||
=> _profiler ?? (_profiler = _container?.TryGetInstance<IProfiler>()
|
||||
?? new LogProfiler(Logger));
|
||||
|
||||
public static ProfilingLogger ProfilingLogger
|
||||
=> _profilingLogger ?? (_profilingLogger = TryGetInstance<ProfilingLogger>(_container))
|
||||
=> _profilingLogger ?? (_profilingLogger = _container?.TryGetInstance<ProfilingLogger>())
|
||||
?? new ProfilingLogger(Logger, Profiler);
|
||||
|
||||
public static IRuntimeState RuntimeState
|
||||
|
||||
@@ -37,6 +37,16 @@ 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>
|
||||
|
||||
@@ -109,6 +109,10 @@ 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>();
|
||||
|
||||
Reference in New Issue
Block a user