Resvolution - Booting

This commit is contained in:
Stephan
2016-08-25 15:09:51 +02:00
parent 9949f07a46
commit 9dcc6b285f
48 changed files with 2282 additions and 2753 deletions

View File

@@ -5,6 +5,7 @@ using Umbraco.Core.Configuration;
using Umbraco.Core.Dictionary;
using Umbraco.Core.Logging;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Plugins;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Strings;
using Umbraco.Core.Sync;
@@ -15,19 +16,20 @@ namespace Umbraco.Core.DependencyInjection
// this class is here to support the transition from singletons and resolvers to injection,
// by providing a static access to singleton services - it is initialized once with a service
// container, in CoreBootManager.
// ideally, it should not exist. practically, time will tell.
// obviously, this is some sort of service locator anti-pattern. ideally, it should not exist.
// practically... time will tell.
public static class Current
{
private static ServiceContainer _container;
public static ServiceContainer Container
internal static ServiceContainer Container
{
get
{
if (_container == null) throw new Exception("No container has been set.");
return _container;
}
internal set // ok to set - don't be stupid
set // ok to set - don't be stupid
{
if (_container != null) throw new Exception("A container has already been set.");
_container = value;
@@ -44,6 +46,7 @@ namespace Umbraco.Core.DependencyInjection
_shortStringHelper = null;
_logger = null;
_profiler = null;
_profilingLogger = null;
Resetted?.Invoke(null, EventArgs.Empty);
}
@@ -52,6 +55,9 @@ namespace Umbraco.Core.DependencyInjection
#region Getters
public static PluginManager PluginManager
=> Container.GetInstance<PluginManager>();
public static UrlSegmentProviderCollection UrlSegmentProviders
=> Container.GetInstance<UrlSegmentProviderCollection>();
@@ -99,6 +105,7 @@ namespace Umbraco.Core.DependencyInjection
private static ILogger _logger;
private static IProfiler _profiler;
private static ProfilingLogger _profilingLogger;
public static ILogger Logger
=> _logger ?? (_logger = _container?.TryGetInstance<ILogger>()
@@ -108,6 +115,10 @@ namespace Umbraco.Core.DependencyInjection
=> _profiler ?? (_profiler = _container?.TryGetInstance<IProfiler>()
?? new LogProfiler(Logger));
public static ProfilingLogger ProfilingLogger
=> _profilingLogger ?? (_profilingLogger = _container?.TryGetInstance<ProfilingLogger>())
?? new ProfilingLogger(Logger, Profiler);
#endregion
}
}

View File

@@ -153,6 +153,8 @@ namespace Umbraco.Core.DependencyInjection
return container.AvailableServices.SingleOrDefault(x => x.ServiceType == typeofTService && x.ServiceName == name);
}
// FIXME or just use names?!
/// <summary>
/// In order for LightInject to deal with enumerables of the same type, each one needs to be registered as their explicit types
/// </summary>
@@ -167,9 +169,14 @@ namespace Umbraco.Core.DependencyInjection
where TLifetime : ILifetime, new()
{
foreach (var type in implementationTypes)
{
container.Register(type, new TLifetime());
}
}
public static void RegisterCollection<TLifetime>(this IServiceContainer container, Func<IServiceFactory, IEnumerable<Type>> implementationTypes)
where TLifetime : ILifetime, new()
{
foreach (var type in implementationTypes(container))
container.Register(type, new TLifetime());
}
/// <summary>
@@ -188,5 +195,11 @@ namespace Umbraco.Core.DependencyInjection
container.Register(type);
}
}
public static void RegisterCollection(this IServiceContainer container, Func<IServiceFactory, IEnumerable<Type>> implementationTypes)
{
foreach (var type in implementationTypes(container))
container.Register(type);
}
}
}

View File

@@ -58,6 +58,27 @@ namespace Umbraco.Core.DependencyInjection
return This;
}
/// <summary>
/// Appends types to the collections.
/// </summary>
/// <param name="types">The types to append.</param>
/// <returns>The builder.</returns>
public TBuilder Append(Func<IServiceFactory, IEnumerable<Type>> types)
{
Configure(list =>
{
foreach (var type in types(Container))
{
// would be detected by CollectionBuilderBase when registering, anyways, but let's fail fast
if (typeof(TItem).IsAssignableFrom(type) == false)
throw new InvalidOperationException($"Cannot register type {type.FullName} as it does not inherit from/implement {typeof(TItem).FullName}.");
if (list.Contains(type)) list.Remove(type);
list.Add(type);
}
});
return This;
}
/// <summary>
/// Appends a type after another type.
/// </summary>