using System; using LightInject; using Umbraco.Core.Cache; using Umbraco.Core.PropertyEditors; using Umbraco.Core.Strings; 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. public static class Current { private static ServiceContainer _container; public 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 { if (_container != null) throw new Exception("A container has already been set."); _container = value; } } internal static bool HasContainer => _container != null; // for UNIT TESTS exclusively! internal static void Reset() { _container = null; Resetted?.Invoke(null, EventArgs.Empty); } internal static event EventHandler Resetted; #region Getters public static UrlSegmentProviderCollection UrlSegmentProviders => Container.GetInstance(); public static CacheRefresherCollection CacheRefreshers => Container.GetInstance(); public static PropertyEditorCollection PropertyEditors => Container.GetInstance(); public static ParameterEditorCollection ParameterEditors => Container.GetInstance(); internal static ValidatorCollection Validators => Container.GetInstance(); #endregion } }