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

62 lines
2.0 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;
2016-08-07 17:08:57 +02:00
using Umbraco.Core.PropertyEditors;
2016-07-28 19:29:30 +02:00
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.
2016-07-29 10:58:57 +02:00
// 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-07-29 10:58:57 +02:00
public 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;
}
internal set // ok to set - don't be stupid
{
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;
Resetted?.Invoke(null, EventArgs.Empty);
}
internal static event EventHandler Resetted;
#region Getters
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-07-29 10:58:57 +02:00
#endregion
2016-07-28 19:29:30 +02:00
}
}