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

80 lines
2.7 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.Models.PublishedContent;
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-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-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>();
2016-07-29 10:58:57 +02:00
#endregion
2016-07-28 19:29:30 +02:00
}
}