Files
Umbraco-CMS/src/Umbraco.Core/DependencyInjection/Current.cs
2016-09-19 16:30:43 +02:00

62 lines
2.0 KiB
C#

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<UrlSegmentProviderCollection>();
public static CacheRefresherCollection CacheRefreshers
=> Container.GetInstance<CacheRefresherCollection>();
public static PropertyEditorCollection PropertyEditors
=> Container.GetInstance<PropertyEditorCollection>();
public static ParameterEditorCollection ParameterEditors
=> Container.GetInstance<ParameterEditorCollection>();
internal static ValidatorCollection Validators
=> Container.GetInstance<ValidatorCollection>();
#endregion
}
}