Files
Umbraco-CMS/src/Umbraco.Core/DependencyInjection/Current.cs

138 lines
5.2 KiB
C#
Raw Normal View History

2016-07-28 19:29:30 +02:00
using System;
using LightInject;
2016-08-13 16:02:35 +02:00
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Dictionary;
2016-08-25 10:23:41 +02:00
using Umbraco.Core.Logging;
using Umbraco.Core.Models.PublishedContent;
2016-08-25 15:09:51 +02:00
using Umbraco.Core.Plugins;
2016-08-07 17:08:57 +02:00
using Umbraco.Core.PropertyEditors;
2016-07-28 19:29:30 +02:00
using Umbraco.Core.Strings;
2016-08-23 11:17:08 +02:00
using Umbraco.Core.Sync;
2016-08-19 11:13:19 +02:00
using Umbraco.Core._Legacy.PackageActions;
2016-07-28 19:29:30 +02:00
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.
2016-08-25 15:09:51 +02:00
// obviously, this is some sort of service locator anti-pattern. ideally, it should not exist.
// practically... time will tell.
2016-07-28 19:29:30 +02:00
public static class Current
{
2016-07-29 10:58:57 +02:00
private static ServiceContainer _container;
2016-07-28 19:29:30 +02:00
2016-08-25 15:09:51 +02:00
internal static ServiceContainer Container
2016-07-28 19:29:30 +02:00
{
get
{
2016-07-29 10:58:57 +02:00
if (_container == null) throw new Exception("No container has been set.");
return _container;
}
2016-08-25 15:09:51 +02:00
set // ok to set - don't be stupid
2016-07-29 10:58:57 +02:00
{
if (_container != null) throw new Exception("A container has already been set.");
_container = value;
2016-07-28 19:29:30 +02:00
}
}
2016-07-29 10:58:57 +02:00
internal static bool HasContainer => _container != null;
// for UNIT TESTS exclusively!
internal static void Reset()
{
_container = null;
2016-08-25 10:23:41 +02:00
_shortStringHelper = null;
2016-08-25 10:23:41 +02:00
_logger = null;
_profiler = null;
2016-08-25 15:09:51 +02:00
_profilingLogger = null;
_applicationContext = null;
2016-08-25 10:23:41 +02:00
2016-07-29 10:58:57 +02:00
Resetted?.Invoke(null, EventArgs.Empty);
}
internal static event EventHandler Resetted;
#region Getters
// fixme - refactor
// some of our tests want to *set* the current application context and bypass the container
// so for the time being we support it, however we should fix our tests
private static ApplicationContext _applicationContext;
public static ApplicationContext ApplicationContext
{
get { return _applicationContext ?? (_applicationContext = Container.GetInstance<ApplicationContext>()); }
set { _applicationContext = value; }
}
2016-08-25 15:09:51 +02:00
public static PluginManager PluginManager
=> Container.GetInstance<PluginManager>();
2016-07-29 10:58:57 +02:00
public static UrlSegmentProviderCollection UrlSegmentProviders
2016-07-28 19:29:30 +02:00
=> Container.GetInstance<UrlSegmentProviderCollection>();
2016-07-29 10:58:57 +02:00
2016-08-13 16:02:35 +02:00
public static CacheRefresherCollection CacheRefreshers
=> Container.GetInstance<CacheRefresherCollection>();
2016-08-07 17:08:57 +02:00
public static PropertyEditorCollection PropertyEditors
=> Container.GetInstance<PropertyEditorCollection>();
2016-08-18 10:02:46 +02:00
public static ParameterEditorCollection ParameterEditors
=> Container.GetInstance<ParameterEditorCollection>();
2016-08-18 10:19:33 +02:00
internal static ValidatorCollection Validators
=> Container.GetInstance<ValidatorCollection>();
2016-08-19 11:13:19 +02:00
internal static PackageActionCollection PackageActions
=> Container.GetInstance<PackageActionCollection>();
internal static PropertyValueConverterCollection PropertyValueConverters
=> Container.GetInstance<PropertyValueConverterCollection>();
internal static IPublishedContentModelFactory PublishedContentModelFactory
=> Container.GetInstance<IPublishedContentModelFactory>();
2016-08-23 11:17:08 +02:00
public static IServerMessenger ServerMessenger
=> Container.GetInstance<IServerMessenger>();
2016-08-24 12:28:31 +02:00
public static IServerRegistrar ServerRegistrar
=> Container.GetInstance<IServerRegistrar>();
public static ICultureDictionaryFactory CultureDictionaryFactory
=> Container.GetInstance<ICultureDictionaryFactory>();
2016-08-25 10:23:41 +02:00
// fixme - refactor
// we don't want Umbraco to die because the container has not been properly initialized,
// for some too-important things such as IShortStringHelper or loggers, so if it's not
// registered we setup a default one. We should really refactor our tests so that it does
// not happen, but hey...
private static IShortStringHelper _shortStringHelper;
public static IShortStringHelper ShortStringHelper
2016-08-25 10:23:41 +02:00
=> _shortStringHelper ?? (_shortStringHelper = _container?.TryGetInstance<IShortStringHelper>()
?? new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(UmbracoConfig.For.UmbracoSettings())));
private static ILogger _logger;
private static IProfiler _profiler;
2016-08-25 15:09:51 +02:00
private static ProfilingLogger _profilingLogger;
2016-08-25 10:23:41 +02:00
public static ILogger Logger
=> _logger ?? (_logger = _container?.TryGetInstance<ILogger>()
?? new DebugDiagnosticsLogger());
public static IProfiler Profiler
=> _profiler ?? (_profiler = _container?.TryGetInstance<IProfiler>()
?? new LogProfiler(Logger));
2016-08-25 15:09:51 +02:00
public static ProfilingLogger ProfilingLogger
=> _profilingLogger ?? (_profilingLogger = _container?.TryGetInstance<ProfilingLogger>())
?? new ProfilingLogger(Logger, Profiler);
2016-07-29 10:58:57 +02:00
#endregion
2016-07-28 19:29:30 +02:00
}
}