Revert "Get rid of container TryGetInstance"

This reverts commit 00b1dda2fe.
This commit is contained in:
Stephan
2018-08-29 18:50:08 +02:00
parent 31a356d98e
commit fed9e27332
5 changed files with 38 additions and 15 deletions

View File

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

View File

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

View File

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

View File

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