diff --git a/src/Umbraco.Core/Composing/Current.cs b/src/Umbraco.Core/Composing/Current.cs index a763b22e36..9e07b42dfb 100644 --- a/src/Umbraco.Core/Composing/Current.cs +++ b/src/Umbraco.Core/Composing/Current.cs @@ -27,7 +27,7 @@ namespace Umbraco.Core.Composing /// public static class Current { - private static IServiceContainer _container; + private static IContainer _container; private static IShortStringHelper _shortStringHelper; private static ILogger _logger; @@ -38,7 +38,7 @@ namespace Umbraco.Core.Composing /// /// Gets or sets the DI container. /// - internal static IServiceContainer Container + internal static IContainer Container { get { diff --git a/src/Umbraco.Core/Composing/IContainer.cs b/src/Umbraco.Core/Composing/IContainer.cs new file mode 100644 index 0000000000..4e9d8df3c5 --- /dev/null +++ b/src/Umbraco.Core/Composing/IContainer.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Umbraco.Core.Composing +{ + public interface IContainer + { + T TryGetInstance(); + T GetInstance(); + object ConcreteContainer { get; } + void RegisterSingleton(Func factory); + void Register(Func factory); + T RegisterCollectionBuilder(); + } +} diff --git a/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs b/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs new file mode 100644 index 0000000000..62e07683a3 --- /dev/null +++ b/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LightInject; + +namespace Umbraco.Core.Composing.LightInject +{ + public class ContainerAdapter : IContainer + { + private readonly IServiceContainer container; + + public object ConcreteContainer => container; + + public void RegisterSingleton(Func factory) + { + container.RegisterSingleton(f => factory(this)); + } + + public void Register(Func factory) + { + container.Register(f => factory(this)); + } + + public T RegisterCollectionBuilder() + { + return container.RegisterCollectionBuilder(); + } + + public ContainerAdapter(IServiceContainer container) + { + this.container = container; + } + + public T TryGetInstance() + { + return container.TryGetInstance(); + } + + public T GetInstance() + { + return container.GetInstance(); + } + } +} diff --git a/src/Umbraco.Core/Composing/LightInjectExtensions.cs b/src/Umbraco.Core/Composing/LightInjectExtensions.cs index 68ba48c803..314e03af84 100644 --- a/src/Umbraco.Core/Composing/LightInjectExtensions.cs +++ b/src/Umbraco.Core/Composing/LightInjectExtensions.cs @@ -47,7 +47,7 @@ namespace Umbraco.Core.Composing container.Register(_ => container); // configure the current container - Current.Container = container; + Current.Container = new LightInject.ContainerAdapter(container); } private class AssemblyScanner : IAssemblyScanner diff --git a/src/Umbraco.Core/IO/MediaFileSystem.cs b/src/Umbraco.Core/IO/MediaFileSystem.cs index 2c9773a9cb..b3fc2ccb61 100644 --- a/src/Umbraco.Core/IO/MediaFileSystem.cs +++ b/src/Umbraco.Core/IO/MediaFileSystem.cs @@ -40,7 +40,7 @@ namespace Umbraco.Core.IO { // due to how FileSystems is written at the moment, the ctor cannot be used to inject // dependencies, so we have to rely on property injection for anything we might need - Current.Container.InjectProperties(this); + ((IServiceContainer)Current.Container.ConcreteContainer).InjectProperties(this); UploadAutoFillProperties = new UploadAutoFillProperties(this, Logger, ContentConfig); } diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index e3ae9e31d5..ac8afb010e 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -166,9 +166,11 @@ + + diff --git a/src/Umbraco.Tests/CoreThings/UdiTests.cs b/src/Umbraco.Tests/CoreThings/UdiTests.cs index 62aa56bd14..4300401e18 100644 --- a/src/Umbraco.Tests/CoreThings/UdiTests.cs +++ b/src/Umbraco.Tests/CoreThings/UdiTests.cs @@ -27,7 +27,7 @@ namespace Umbraco.Tests.CoreThings var globalSettings = SettingsForTests.GenerateMockGlobalSettings(); container.Setup(x => x.GetInstance(typeof (TypeLoader))).Returns( new TypeLoader(NullCacheProvider.Instance, globalSettings, new ProfilingLogger(Mock.Of(), Mock.Of()))); - Current.Container = container.Object; + Current.Container = new Core.Composing.LightInject.ContainerAdapter(container.Object); Udi.ResetUdiTypes(); } diff --git a/src/Umbraco.Tests/Models/VariationTests.cs b/src/Umbraco.Tests/Models/VariationTests.cs index 044f8fa0cd..0b0ad8c7ec 100644 --- a/src/Umbraco.Tests/Models/VariationTests.cs +++ b/src/Umbraco.Tests/Models/VariationTests.cs @@ -4,6 +4,7 @@ using Moq; using NUnit.Framework; using Umbraco.Core.Cache; using Umbraco.Core.Composing; +using Umbraco.Core.Composing.LightInject; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.PropertyEditors; @@ -29,7 +30,7 @@ namespace Umbraco.Tests.Models Current.Reset(); var container = Mock.Of(); - Current.Container = container; + Current.Container = new ContainerAdapter(container); var dataEditors = new DataEditorCollection(new IDataEditor[] { diff --git a/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs b/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs index 842fe50e04..64fa0a46ab 100644 --- a/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs @@ -13,6 +13,7 @@ using Umbraco.Core.Scoping; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Core.Composing; +using Umbraco.Core.Composing.LightInject; using Umbraco.Core.Persistence.Mappers; using Umbraco.Core.Services; @@ -31,9 +32,10 @@ namespace Umbraco.Tests.Scoping DoThing2 = null; DoThing3 = null; - Current.Container = new ServiceContainer(); + var lightinjectContainer = new ServiceContainer(); + Current.Container = new ContainerAdapter(lightinjectContainer); - _testObjects = new TestObjects(Current.Container); + _testObjects = new TestObjects(lightinjectContainer); Current.Container.RegisterSingleton(f => Current.Container); Current.Container.RegisterSingleton(factory => new FileSystems(factory.TryGetInstance())); Current.Container.RegisterCollectionBuilder(); diff --git a/src/Umbraco.Tests/Web/TemplateUtilitiesTests.cs b/src/Umbraco.Tests/Web/TemplateUtilitiesTests.cs index c6f7d8551e..867d4574f1 100644 --- a/src/Umbraco.Tests/Web/TemplateUtilitiesTests.cs +++ b/src/Umbraco.Tests/Web/TemplateUtilitiesTests.cs @@ -8,6 +8,7 @@ using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Composing; +using Umbraco.Core.Composing.LightInject; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Logging; using Umbraco.Core.Models; @@ -43,7 +44,7 @@ namespace Umbraco.Tests.Web container.Setup(x => x.GetInstance(typeof(TypeLoader))).Returns( new TypeLoader(NullCacheProvider.Instance, SettingsForTests.GenerateMockGlobalSettings(), new ProfilingLogger(Mock.Of(), Mock.Of()))); container.Setup(x => x.GetInstance(typeof (ServiceContext))).Returns(serviceContext); - Current.Container = container.Object; + Current.Container = new ContainerAdapter(container.Object); Umbraco.Web.Composing.Current.UmbracoContextAccessor = new TestUmbracoContextAccessor(); diff --git a/src/Umbraco.Web/Composing/Current.cs b/src/Umbraco.Web/Composing/Current.cs index d295cf3d55..8c7f4e87ce 100644 --- a/src/Umbraco.Web/Composing/Current.cs +++ b/src/Umbraco.Web/Composing/Current.cs @@ -58,7 +58,7 @@ namespace Umbraco.Web.Composing /// /// Gets the DI container. /// - internal static IServiceContainer Container + internal static IContainer Container => CoreCurrent.Container; #region Temp & Special diff --git a/src/Umbraco.Web/Editors/SectionController.cs b/src/Umbraco.Web/Editors/SectionController.cs index 76821cfc33..1bed96bc0f 100644 --- a/src/Umbraco.Web/Editors/SectionController.cs +++ b/src/Umbraco.Web/Editors/SectionController.cs @@ -3,6 +3,7 @@ using AutoMapper; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Mvc; using System.Linq; +using LightInject; using Umbraco.Core.Composing; using Umbraco.Core.Models; using Umbraco.Web.Trees; @@ -31,7 +32,7 @@ namespace Umbraco.Web.Editors // since tree's by nature are controllers and require request contextual data - and then we have to // remember to inject properties - nasty indeed var appTreeController = new ApplicationTreeController(); - Current.Container.InjectProperties(appTreeController); + ((IServiceContainer)Current.Container.ConcreteContainer).InjectProperties(appTreeController); appTreeController.ControllerContext = ControllerContext; var dashboards = dashboardHelper.GetDashboards(Security.CurrentUser); diff --git a/src/Umbraco.Web/Security/MembershipHelper.cs b/src/Umbraco.Web/Security/MembershipHelper.cs index 6d28680877..7a56679086 100644 --- a/src/Umbraco.Web/Security/MembershipHelper.cs +++ b/src/Umbraco.Web/Security/MembershipHelper.cs @@ -73,7 +73,7 @@ namespace Umbraco.Web.Security // helpers are *not* instanciated by the container so we have to // get our dependencies injected manually, through properties. - Current.Container.InjectProperties(this); + ((IServiceContainer)Current.Container.ConcreteContainer).InjectProperties(this); } // used everywhere @@ -96,7 +96,7 @@ namespace Umbraco.Web.Security // helpers are *not* instanciated by the container so we have to // get our dependencies injected manually, through properties. - Current.Container.InjectProperties(this); + ((IServiceContainer)Current.Container.ConcreteContainer).InjectProperties(this); } #endregion