From 4c201bcaaa2cd3b9c4e228e2eb7db1836880da10 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Tue, 12 Jun 2018 23:12:44 +0200 Subject: [PATCH 01/15] HttpModules able to get injection. UmbracoModule + rest (if implemented) gets dependencies injected through ContainerUmbracoModule. --- src/Umbraco.Web/Routing/PublishedRouter.cs | 2 +- src/Umbraco.Web/Runtime/WebRuntime.cs | 2 + src/Umbraco.Web/UmbracoModule.cs | 94 ++++++++++++++++++---- 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/src/Umbraco.Web/Routing/PublishedRouter.cs b/src/Umbraco.Web/Routing/PublishedRouter.cs index fdc8540aa0..8e7d786e93 100644 --- a/src/Umbraco.Web/Routing/PublishedRouter.cs +++ b/src/Umbraco.Web/Routing/PublishedRouter.cs @@ -20,7 +20,7 @@ namespace Umbraco.Web.Routing { // fixme - make this public // fixme - making sense to have an interface? - internal class PublishedRouter + public class PublishedRouter { private readonly IWebRoutingSection _webRoutingSection; private readonly ContentFinderCollection _contentFinders; diff --git a/src/Umbraco.Web/Runtime/WebRuntime.cs b/src/Umbraco.Web/Runtime/WebRuntime.cs index 78610c01b8..9d08a4d85b 100644 --- a/src/Umbraco.Web/Runtime/WebRuntime.cs +++ b/src/Umbraco.Web/Runtime/WebRuntime.cs @@ -56,6 +56,8 @@ namespace Umbraco.Web.Runtime { base.Compose(container); + container.Register(); + // replace CoreRuntime's IProfiler registration container.RegisterSingleton(_ => _webProfiler); diff --git a/src/Umbraco.Web/UmbracoModule.cs b/src/Umbraco.Web/UmbracoModule.cs index d5fb8dee92..d41ad9ccd0 100644 --- a/src/Umbraco.Web/UmbracoModule.cs +++ b/src/Umbraco.Web/UmbracoModule.cs @@ -4,8 +4,10 @@ using System.Collections.Generic; using System.IO; using System.Text; using System.Web; +using System.Web.Mvc; using System.Web.Routing; using LightInject; +using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.IO; @@ -22,8 +24,38 @@ using Umbraco.Core.Services; using Umbraco.Web.Composing; using Umbraco.Web.PublishedCache; +[assembly: PreApplicationStartMethod(typeof(Umbraco.Web.ContainerUmbracoModule), "Start")] namespace Umbraco.Web { + public class ContainerUmbracoModule : IHttpModule + { + private UmbracoModule umbracoModule; + + public static void Start() + { + DynamicModuleUtility.RegisterModule(typeof(ContainerUmbracoModule)); + } + + public ContainerUmbracoModule() + { + + } + + public void Init(HttpApplication context) + { + // Should be extended with lazy list of modules from registry + // Example here: https://haacked.com/archive/2011/06/03/dependency-injection-with-asp-net-httpmodules.aspx/ + + umbracoModule = Current.Container.GetInstance(); + umbracoModule.Init(context); + } + + public void Dispose() + { + umbracoModule.Dispose(); + } + } + // also look at IOHelper.ResolveUrlsFromTextString - nightmarish?! // context.RewritePath supports ~/ or else must begin with /vdir @@ -38,44 +70,74 @@ namespace Umbraco.Web // get our dependencies injected manually, through properties, in // Init(). works for dependencies that are singletons. - [Inject] public IUmbracoSettingsSection UmbracoSettings { get; set; } - [Inject] public IGlobalSettings GlobalSettings { get; set; } - [Inject] public IUmbracoContextAccessor UmbracoContextAccessor { get; set; } - [Inject] public IPublishedSnapshotService PublishedSnapshotService { get; set; } - [Inject] public IUserService UserService { get; set; } - [Inject] public UrlProviderCollection UrlProviders { get; set; } - [Inject] public IRuntimeState Runtime { get; set; } - [Inject] public ILogger Logger { get; set; } - [Inject] internal PublishedRouter PublishedRouter { get; set; } - [Inject] internal IUmbracoDatabaseFactory DatabaseFactory { get; set; } - [Inject] internal IVariationContextAccessor VariationContextAccessor { get; set; } - + #endregion - public UmbracoModule() + public UmbracoModule() + : this( + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance() + ) + { + } + + public UmbracoModule( + IUmbracoSettingsSection umbracoSettings, + IGlobalSettings globalSettings, + IUmbracoContextAccessor umbracoContextAccessor, + IPublishedSnapshotService publishedSnapshotService, + IUserService userService, + UrlProviderCollection urlProviders, + IRuntimeState runtime, + ILogger logger, + PublishedRouter publishedRouter, + IUmbracoDatabaseFactory databaseFactory, + IVariationContextAccessor variationContextAccessor + ) { _combinedRouteCollection = new Lazy(CreateRouteCollection); + + UmbracoSettings = umbracoSettings; + GlobalSettings = globalSettings; + UmbracoContextAccessor = umbracoContextAccessor; + PublishedSnapshotService = publishedSnapshotService; + UserService = userService; + UrlProviders = urlProviders; + Runtime = runtime; + Logger = logger; + PublishedRouter = publishedRouter; + DatabaseFactory = databaseFactory; + VariationContextAccessor = variationContextAccessor; } #region HttpModule event handlers @@ -543,10 +605,10 @@ namespace Umbraco.Web }; return; } - - // modules are *not* instanciated by the container so we have to + + // modules **are** instanciated by the container so we **don't** have to // get our dependencies injected manually, through properties. - Core.Composing.Current.Container.InjectProperties(this); + //Core.Composing.Current.Container.InjectProperties(this); app.BeginRequest += (sender, e) => { From a8c5cd425d14e936eaefaa2c082b390187e52ce3 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Thu, 14 Jun 2018 18:46:25 +0200 Subject: [PATCH 02/15] DataTypeValidateAttribute now uses bastard injection. Notes on the UmbracoModule. Need to abstract resolving via Current.Container to remove LightInject refs. --- .../Routing/UmbracoModuleTests.cs | 3 ++- .../Editors/DataTypeValidateAttribute.cs | 27 ++++++++++++++----- src/Umbraco.Web/UmbracoModule.cs | 3 ++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs b/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs index 703179b184..b87f092a1d 100644 --- a/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs +++ b/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs @@ -23,7 +23,8 @@ namespace Umbraco.Tests.Routing public override void SetUp() { base.SetUp(); - + + // fixme - use the full injected ctor, then we can ditch this one. //create the module _module = new UmbracoModule { diff --git a/src/Umbraco.Web/Editors/DataTypeValidateAttribute.cs b/src/Umbraco.Web/Editors/DataTypeValidateAttribute.cs index f8f2b5ac23..43093f563c 100644 --- a/src/Umbraco.Web/Editors/DataTypeValidateAttribute.cs +++ b/src/Umbraco.Web/Editors/DataTypeValidateAttribute.cs @@ -5,14 +5,15 @@ using System.Net.Http; using System.Web.Http.Controllers; using System.Web.Http.Filters; using AutoMapper; -using LightInject; using Umbraco.Core; +using Umbraco.Core.Composing; using Umbraco.Core.Models; using Umbraco.Core.PropertyEditors; using Umbraco.Core.Services; -using Umbraco.Web.Composing; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.WebApi; + +using LightInject; namespace Umbraco.Web.Editors { @@ -21,13 +22,25 @@ namespace Umbraco.Web.Editors /// internal sealed class DataTypeValidateAttribute : ActionFilterAttribute { - // LightInject can inject dependencies into properties + public IDataTypeService DataTypeService { get; } - [Inject] - public IDataTypeService DataTypeService { get; set; } + public PropertyEditorCollection PropertyEditors { get; } - [Inject] - public PropertyEditorCollection PropertyEditors { get; set; } + public DataTypeValidateAttribute() + : this(Current.Container.GetInstance(), Current.Container.GetInstance()) + { + } + + /// + /// For use in unit tests. Not possible to use as attribute ctor. + /// + /// + /// + public DataTypeValidateAttribute(IDataTypeService dataTypeService, PropertyEditorCollection propertyEditors) + { + DataTypeService = dataTypeService; + PropertyEditors = propertyEditors; + } public override void OnActionExecuting(HttpActionContext actionContext) { diff --git a/src/Umbraco.Web/UmbracoModule.cs b/src/Umbraco.Web/UmbracoModule.cs index d41ad9ccd0..0efb2a2f85 100644 --- a/src/Umbraco.Web/UmbracoModule.cs +++ b/src/Umbraco.Web/UmbracoModule.cs @@ -93,7 +93,8 @@ namespace Umbraco.Web internal IVariationContextAccessor VariationContextAccessor { get; set; } #endregion - + + // fixme - delete, just one usage in a test. public UmbracoModule() : this( Current.Container.GetInstance(), From 9b1bd3be60764675d00557044fd8adbf0fedabfc Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Thu, 14 Jun 2018 19:14:39 +0200 Subject: [PATCH 03/15] UmbracoModule parameterless ctor removed. More notes / fixme. --- .../Routing/UmbracoModuleTests.cs | 31 ++++++++++++++----- src/Umbraco.Web/UmbracoModule.cs | 18 ----------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs b/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs index b87f092a1d..8cbb790d8e 100644 --- a/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs +++ b/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs @@ -10,8 +10,16 @@ using Umbraco.Web; using Umbraco.Core.IO; using Umbraco.Core.Logging; using Umbraco.Core.Sync; -using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; +using Umbraco.Web.PublishedCache; +using Umbraco.Web.Routing; +// fixme - abstract container +using LightInject; + namespace Umbraco.Tests.Routing { [TestFixture] @@ -24,13 +32,22 @@ namespace Umbraco.Tests.Routing { base.SetUp(); - // fixme - use the full injected ctor, then we can ditch this one. + // fixme - be able to get the UmbracoModule from the container. any reason settings were from testobjects? //create the module - _module = new UmbracoModule - { - GlobalSettings = TestObjects.GetGlobalSettings(), - Logger = Mock.Of() - }; + _module = new UmbracoModule + ( + TestObjects.GetUmbracoSettings(), + TestObjects.GetGlobalSettings(), + Mock.Of(), + Container.GetInstance(), + Container.GetInstance(), + new UrlProviderCollection(new IUrlProvider[0]), + Container.GetInstance(), + Mock.Of(), + null, // fixme - PublishedRouter complexities... + Container.GetInstance(), + Mock.Of() + ); var runtime = new RuntimeState(_module.Logger, new Lazy(), new Lazy(), Mock.Of(), _module.GlobalSettings); _module.Runtime = runtime; diff --git a/src/Umbraco.Web/UmbracoModule.cs b/src/Umbraco.Web/UmbracoModule.cs index 0efb2a2f85..7a12fd1130 100644 --- a/src/Umbraco.Web/UmbracoModule.cs +++ b/src/Umbraco.Web/UmbracoModule.cs @@ -94,24 +94,6 @@ namespace Umbraco.Web #endregion - // fixme - delete, just one usage in a test. - public UmbracoModule() - : this( - Current.Container.GetInstance(), - Current.Container.GetInstance(), - Current.Container.GetInstance(), - Current.Container.GetInstance(), - Current.Container.GetInstance(), - Current.Container.GetInstance(), - Current.Container.GetInstance(), - Current.Container.GetInstance(), - Current.Container.GetInstance(), - Current.Container.GetInstance(), - Current.Container.GetInstance() - ) - { - } - public UmbracoModule( IUmbracoSettingsSection umbracoSettings, IGlobalSettings globalSettings, From e3d77fb012a0d28eafa0468d687234846e7371af Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Sat, 16 Jun 2018 13:13:29 +0200 Subject: [PATCH 04/15] Current.Container abstracted. Builds, but fails for collection builder tests. --- src/Umbraco.Core/Composing/Current.cs | 4 +- src/Umbraco.Core/Composing/IContainer.cs | 18 ++++++++ .../Composing/LightInject/ContainerAdapter.cs | 46 +++++++++++++++++++ .../Composing/LightInjectExtensions.cs | 2 +- src/Umbraco.Core/IO/MediaFileSystem.cs | 2 +- src/Umbraco.Core/Umbraco.Core.csproj | 2 + src/Umbraco.Tests/CoreThings/UdiTests.cs | 2 +- src/Umbraco.Tests/Models/VariationTests.cs | 3 +- .../Scoping/ScopeEventDispatcherTests.cs | 6 ++- .../Web/TemplateUtilitiesTests.cs | 3 +- src/Umbraco.Web/Composing/Current.cs | 2 +- src/Umbraco.Web/Editors/SectionController.cs | 3 +- src/Umbraco.Web/Security/MembershipHelper.cs | 4 +- 13 files changed, 84 insertions(+), 13 deletions(-) create mode 100644 src/Umbraco.Core/Composing/IContainer.cs create mode 100644 src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs 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 From 0e37b3b61b57c30fda197b9fd85d6ab4bc553b09 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Tue, 19 Jun 2018 18:19:10 +0200 Subject: [PATCH 05/15] ScopeEventDispatcherTest needs IServiceContainer to be registered. --- src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs b/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs index 64fa0a46ab..b295edfb8b 100644 --- a/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs @@ -36,6 +36,10 @@ namespace Umbraco.Tests.Scoping Current.Container = new ContainerAdapter(lightinjectContainer); _testObjects = new TestObjects(lightinjectContainer); + + // fixme - move to container factory? + Current.Container.RegisterSingleton(f => (IServiceContainer)Current.Container.ConcreteContainer); + Current.Container.RegisterSingleton(f => Current.Container); Current.Container.RegisterSingleton(factory => new FileSystems(factory.TryGetInstance())); Current.Container.RegisterCollectionBuilder(); From e4aca5f5d81bff5cc8969d7de2a2d012db6373d1 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Tue, 19 Jun 2018 18:53:25 +0200 Subject: [PATCH 06/15] Several property injections removed from Mvc artifacts and base classes --- .../Controllers/UmbLoginController.cs | 14 +++++ .../Controllers/UmbLoginStatusController.cs | 15 +++++- .../Controllers/UmbProfileController.cs | 13 +++++ .../Controllers/UmbRegisterController.cs | 15 +++++- .../Editors/AuthenticationController.cs | 12 ++++- .../Editors/BackOfficeController.cs | 9 +++- .../Editors/DashboardController.cs | 13 ++++- src/Umbraco.Web/Mvc/PluginController.cs | 29 +++++++--- src/Umbraco.Web/Mvc/RenderMvcController.cs | 8 +++ src/Umbraco.Web/Mvc/SurfaceController.cs | 14 +++++ .../Mvc/UmbracoAuthorizedController.cs | 18 ++++++- src/Umbraco.Web/Mvc/UmbracoController.cs | 34 ++++++++---- .../Mvc/UmbracoViewPageOfTModel.cs | 17 ++++-- .../WebApi/UmbracoApiController.cs | 19 ++++++- .../WebApi/UmbracoApiControllerBase.cs | 53 +++++++++++++------ .../WebApi/UmbracoAuthorizedApiController.cs | 17 +++++- src/Umbraco.Web/WebServices/TagsController.cs | 15 ++++++ 17 files changed, 268 insertions(+), 47 deletions(-) diff --git a/src/Umbraco.Web/Controllers/UmbLoginController.cs b/src/Umbraco.Web/Controllers/UmbLoginController.cs index 2b70f927b7..54703e28bd 100644 --- a/src/Umbraco.Web/Controllers/UmbLoginController.cs +++ b/src/Umbraco.Web/Controllers/UmbLoginController.cs @@ -2,11 +2,25 @@ using Umbraco.Web.Models; using Umbraco.Web.Mvc; using Umbraco.Core; +using Umbraco.Core.Cache; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; namespace Umbraco.Web.Controllers { public class UmbLoginController : SurfaceController { + // fixme - delete? + public UmbLoginController() + { + } + + public UmbLoginController(UmbracoContext umbracoContext, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger) + : base(umbracoContext, databaseFactory, services, applicationCache, logger, profilingLogger) + { + } + [HttpPost] public ActionResult HandleLogin([Bind(Prefix = "loginModel")]LoginModel model) { diff --git a/src/Umbraco.Web/Controllers/UmbLoginStatusController.cs b/src/Umbraco.Web/Controllers/UmbLoginStatusController.cs index 0364359673..8df46078ff 100644 --- a/src/Umbraco.Web/Controllers/UmbLoginStatusController.cs +++ b/src/Umbraco.Web/Controllers/UmbLoginStatusController.cs @@ -3,12 +3,25 @@ using System.Web.Security; using Umbraco.Web.Models; using Umbraco.Web.Mvc; using Umbraco.Core; +using Umbraco.Core.Cache; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; namespace Umbraco.Web.Controllers { [MemberAuthorize] public class UmbLoginStatusController : SurfaceController - { + { + // fixme - delete? + public UmbLoginStatusController() + { + } + + public UmbLoginStatusController(UmbracoContext umbracoContext, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger) : base(umbracoContext, databaseFactory, services, applicationCache, logger, profilingLogger) + { + } + [HttpPost] public ActionResult HandleLogout([Bind(Prefix = "logoutModel")]PostRedirectModel model) { diff --git a/src/Umbraco.Web/Controllers/UmbProfileController.cs b/src/Umbraco.Web/Controllers/UmbProfileController.cs index 27c5c32a02..5cb5d1b83e 100644 --- a/src/Umbraco.Web/Controllers/UmbProfileController.cs +++ b/src/Umbraco.Web/Controllers/UmbProfileController.cs @@ -4,12 +4,25 @@ using Umbraco.Web.Models; using Umbraco.Web.Mvc; using Umbraco.Core.Security; using Umbraco.Core; +using Umbraco.Core.Cache; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; namespace Umbraco.Web.Controllers { [MemberAuthorize] public class UmbProfileController : SurfaceController { + // fixme - delete? + public UmbProfileController() + { + } + + public UmbProfileController(UmbracoContext umbracoContext, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger) : base(umbracoContext, databaseFactory, services, applicationCache, logger, profilingLogger) + { + } + [HttpPost] public ActionResult HandleUpdateProfile([Bind(Prefix = "profileModel")] ProfileModel model) { diff --git a/src/Umbraco.Web/Controllers/UmbRegisterController.cs b/src/Umbraco.Web/Controllers/UmbRegisterController.cs index 64f54dd465..a0960e21ee 100644 --- a/src/Umbraco.Web/Controllers/UmbRegisterController.cs +++ b/src/Umbraco.Web/Controllers/UmbRegisterController.cs @@ -2,13 +2,26 @@ using System.Web.Mvc; using System.Web.Security; using Umbraco.Core; +using Umbraco.Core.Cache; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; using Umbraco.Web.Models; using Umbraco.Web.Mvc; namespace Umbraco.Web.Controllers { public class UmbRegisterController : SurfaceController - { + { + // fixme - delete? + public UmbRegisterController() + { + } + + public UmbRegisterController(UmbracoContext umbracoContext, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger) : base(umbracoContext, databaseFactory, services, applicationCache, logger, profilingLogger) + { + } + [HttpPost] public ActionResult HandleRegisterMember([Bind(Prefix = "registerModel")]RegisterModel model) { diff --git a/src/Umbraco.Web/Editors/AuthenticationController.cs b/src/Umbraco.Web/Editors/AuthenticationController.cs index 50cad49657..f4066520e2 100644 --- a/src/Umbraco.Web/Editors/AuthenticationController.cs +++ b/src/Umbraco.Web/Editors/AuthenticationController.cs @@ -11,8 +11,8 @@ using System.Web.Mvc; using AutoMapper; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; -using Microsoft.Owin; using Umbraco.Core; +using Umbraco.Core.Cache; using Umbraco.Core.Models; using Umbraco.Core.Models.Identity; using Umbraco.Core.Security; @@ -26,6 +26,7 @@ using Umbraco.Web.WebApi; using Umbraco.Web.WebApi.Filters; using Umbraco.Core.Configuration; using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; using Umbraco.Web.Composing; using IUser = Umbraco.Core.Models.Membership.IUser; @@ -53,6 +54,15 @@ namespace Umbraco.Web.Editors get { return _signInManager ?? (_signInManager = TryGetOwinContext().Result.GetBackOfficeSignInManager()); } } + public AuthenticationController() + { + } + + public AuthenticationController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger, IRuntimeState runtimeState) + : base(globalSettings, umbracoContext, sqlContext, services, applicationCache, logger, profilingLogger, runtimeState) + { + } + /// /// Returns the configuration for the backoffice user membership provider - used to configure the change password dialog /// diff --git a/src/Umbraco.Web/Editors/BackOfficeController.cs b/src/Umbraco.Web/Editors/BackOfficeController.cs index 1988808d23..f251956649 100644 --- a/src/Umbraco.Web/Editors/BackOfficeController.cs +++ b/src/Umbraco.Web/Editors/BackOfficeController.cs @@ -23,6 +23,7 @@ using Umbraco.Core.Logging; using Umbraco.Core.Manifest; using Umbraco.Core.Models.Identity; using Umbraco.Core.Models.Membership; +using Umbraco.Core.Persistence; using Umbraco.Core.Security; using Umbraco.Web.Models; using Umbraco.Web.Mvc; @@ -55,7 +56,13 @@ namespace Umbraco.Web.Editors private const string TokenPasswordResetCode = "PasswordResetCode"; private static readonly string[] TempDataTokenNames = { TokenExternalSignInError, TokenPasswordResetCode }; - public BackOfficeController(ManifestParser manifestParser, UmbracoFeatures features) + //public BackOfficeController(ManifestParser manifestParser, UmbracoFeatures features) + //{ + // _manifestParser = manifestParser; + // _features = features; + //} + + public BackOfficeController(ManifestParser manifestParser, UmbracoFeatures features, IGlobalSettings globalSettings, UmbracoContext umbracoContext, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger) : base(globalSettings, umbracoContext, databaseFactory, services, applicationCache, logger, profilingLogger) { _manifestParser = manifestParser; _features = features; diff --git a/src/Umbraco.Web/Editors/DashboardController.cs b/src/Umbraco.Web/Editors/DashboardController.cs index d23e262412..37c72d2e02 100644 --- a/src/Umbraco.Web/Editors/DashboardController.cs +++ b/src/Umbraco.Web/Editors/DashboardController.cs @@ -3,8 +3,6 @@ using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Mvc; -using System.Linq; -using Umbraco.Core.IO; using Newtonsoft.Json.Linq; using System.Threading.Tasks; using System.Net.Http; @@ -16,6 +14,8 @@ using Umbraco.Core.Cache; using Umbraco.Web.WebApi; using Umbraco.Web.WebApi.Filters; using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; namespace Umbraco.Web.Editors { @@ -27,6 +27,15 @@ namespace Umbraco.Web.Editors [WebApi.UmbracoAuthorize] public class DashboardController : UmbracoApiController { + public DashboardController() + { + } + + public DashboardController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger, IRuntimeState runtimeState) + : base(globalSettings, umbracoContext, sqlContext, services, applicationCache, logger, profilingLogger, runtimeState) + { + } + //we have baseurl as a param to make previewing easier, so we can test with a dev domain from client side [ValidateAngularAntiForgeryToken] public async Task GetRemoteDashboardContent(string section, string baseUrl = "https://dashboard.umbraco.org/") diff --git a/src/Umbraco.Web/Mvc/PluginController.cs b/src/Umbraco.Web/Mvc/PluginController.cs index 6b5f111585..6e44cca112 100644 --- a/src/Umbraco.Web/Mvc/PluginController.cs +++ b/src/Umbraco.Web/Mvc/PluginController.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Concurrent; using System.Web.Mvc; -using LightInject; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Logging; @@ -36,37 +35,31 @@ namespace Umbraco.Web.Mvc /// /// Gets or sets the Umbraco context. /// - [Inject] public virtual UmbracoContext UmbracoContext { get; set; } /// /// Gets or sets the database context. /// - [Inject] public IUmbracoDatabaseFactory DatabaseFactory { get; set; } /// /// Gets or sets the services context. /// - [Inject] public ServiceContext Services { get; set; } /// /// Gets or sets the application cache. /// - [Inject] public CacheHelper ApplicationCache { get; set; } /// /// Gets or sets the logger. /// - [Inject] public ILogger Logger { get; set; } /// /// Gets or sets the profiling logger. /// - [Inject] public ProfilingLogger ProfilingLogger { get; set; } /// @@ -95,6 +88,28 @@ namespace Umbraco.Web.Mvc /// internal PluginControllerMetadata Metadata => GetMetadata(GetType()); + protected PluginController() + : this( + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance() + ) + { + } + + protected PluginController(UmbracoContext umbracoContext, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger) + { + UmbracoContext = umbracoContext; + DatabaseFactory = databaseFactory; + Services = services; + ApplicationCache = applicationCache; + Logger = logger; + ProfilingLogger = profilingLogger; + } + /// /// Gets metadata for a controller type. /// diff --git a/src/Umbraco.Web/Mvc/RenderMvcController.cs b/src/Umbraco.Web/Mvc/RenderMvcController.cs index e7f336dcde..8077dd48bf 100644 --- a/src/Umbraco.Web/Mvc/RenderMvcController.cs +++ b/src/Umbraco.Web/Mvc/RenderMvcController.cs @@ -2,8 +2,10 @@ using System.Web.Mvc; using Umbraco.Core; using Umbraco.Core.Cache; +using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.Persistence; using Umbraco.Core.Services; using Umbraco.Web.Models; using Umbraco.Web.Routing; @@ -18,11 +20,17 @@ namespace Umbraco.Web.Mvc { private PublishedRequest _publishedRequest; + // fixme - delete? public RenderMvcController() { ActionInvoker = new RenderActionInvoker(); } + public RenderMvcController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger) : base(globalSettings, umbracoContext, databaseFactory, services, applicationCache, logger, profilingLogger) + { + ActionInvoker = new RenderActionInvoker(); + } + /// /// Gets the Umbraco context. /// diff --git a/src/Umbraco.Web/Mvc/SurfaceController.cs b/src/Umbraco.Web/Mvc/SurfaceController.cs index 043841f279..bb7538ebe8 100644 --- a/src/Umbraco.Web/Mvc/SurfaceController.cs +++ b/src/Umbraco.Web/Mvc/SurfaceController.cs @@ -1,7 +1,11 @@ using System; using Umbraco.Core; using System.Collections.Specialized; +using Umbraco.Core.Cache; +using Umbraco.Core.Logging; using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; namespace Umbraco.Web.Mvc { @@ -12,6 +16,16 @@ namespace Umbraco.Web.Mvc [MergeParentContextViewData] public abstract class SurfaceController : PluginController { + // fixme - delete? + protected SurfaceController() + { + } + + protected SurfaceController(UmbracoContext umbracoContext, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger) + : base(umbracoContext, databaseFactory, services, applicationCache, logger, profilingLogger) + { + } + /// /// Redirects to the Umbraco page with the given id /// diff --git a/src/Umbraco.Web/Mvc/UmbracoAuthorizedController.cs b/src/Umbraco.Web/Mvc/UmbracoAuthorizedController.cs index d0e3ec7b61..e9d287f0df 100644 --- a/src/Umbraco.Web/Mvc/UmbracoAuthorizedController.cs +++ b/src/Umbraco.Web/Mvc/UmbracoAuthorizedController.cs @@ -1,4 +1,10 @@ -namespace Umbraco.Web.Mvc +using Umbraco.Core.Cache; +using Umbraco.Core.Configuration; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; + +namespace Umbraco.Web.Mvc { /// /// Provides a base class for authorized Umbraco controllers. @@ -10,5 +16,13 @@ [UmbracoAuthorize] [DisableBrowserCache] public abstract class UmbracoAuthorizedController : UmbracoController - { } + { + protected UmbracoAuthorizedController() + { + } + + protected UmbracoAuthorizedController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger) : base(globalSettings, umbracoContext, databaseFactory, services, applicationCache, logger, profilingLogger) + { + } + } } diff --git a/src/Umbraco.Web/Mvc/UmbracoController.cs b/src/Umbraco.Web/Mvc/UmbracoController.cs index 6f4034fe85..520724bcaf 100644 --- a/src/Umbraco.Web/Mvc/UmbracoController.cs +++ b/src/Umbraco.Web/Mvc/UmbracoController.cs @@ -1,10 +1,9 @@ using System; using System.Web; using System.Web.Mvc; -using LightInject; using Microsoft.Owin; -using Umbraco.Core; using Umbraco.Core.Cache; +using Umbraco.Core.Composing; using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Persistence; @@ -33,43 +32,36 @@ namespace Umbraco.Web.Mvc /// /// Gets or sets the Umbraco context. /// - [Inject] public virtual IGlobalSettings GlobalSettings { get; set; } /// /// Gets or sets the Umbraco context. /// - [Inject] public virtual UmbracoContext UmbracoContext { get; set; } /// /// Gets or sets the database context. /// - [Inject] public IUmbracoDatabaseFactory DatabaseFactory { get; set; } /// /// Gets or sets the services context. /// - [Inject] public ServiceContext Services { get; set; } /// /// Gets or sets the application cache. /// - [Inject] public CacheHelper ApplicationCache { get; set; } /// /// Gets or sets the logger. /// - [Inject] public ILogger Logger { get; set; } /// /// Gets or sets the profiling logger. /// - [Inject] public ProfilingLogger ProfilingLogger { get; set; } protected IOwinContext OwinContext => Request.GetOwinContext(); @@ -89,5 +81,29 @@ namespace Umbraco.Web.Mvc /// Gets the web security helper. /// public virtual WebSecurity Security => UmbracoContext.Security; + + protected UmbracoController() + : this( + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance() + ) + { + } + + protected UmbracoController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger) + { + GlobalSettings = globalSettings; + UmbracoContext = umbracoContext; + DatabaseFactory = databaseFactory; + Services = services; + ApplicationCache = applicationCache; + Logger = logger; + ProfilingLogger = profilingLogger; + } } } diff --git a/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs b/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs index f817b74120..25987d6f30 100644 --- a/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs +++ b/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs @@ -3,7 +3,6 @@ using System.Text; using System.Web; using System.Web.Mvc; using System.Web.WebPages; -using LightInject; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Configuration; @@ -34,13 +33,11 @@ namespace Umbraco.Web.Mvc /// /// Gets or sets the database context. /// - [Inject] public ServiceContext Services { get; set; } /// /// Gets or sets the application cache. /// - [Inject] public CacheHelper ApplicationCache { get; set; } // fixme @@ -109,6 +106,20 @@ namespace Umbraco.Web.Mvc /// public MembershipHelper Members => Umbraco.MembershipHelper; + protected UmbracoViewPage() + : this( + Current.Container.GetInstance(), + Current.Container.GetInstance() + ) + { + } + + protected UmbracoViewPage(ServiceContext services, CacheHelper applicationCache) + { + Services = services; + ApplicationCache = applicationCache; + } + // view logic below: /// diff --git a/src/Umbraco.Web/WebApi/UmbracoApiController.cs b/src/Umbraco.Web/WebApi/UmbracoApiController.cs index 3616f032d8..61c3f89d41 100644 --- a/src/Umbraco.Web/WebApi/UmbracoApiController.cs +++ b/src/Umbraco.Web/WebApi/UmbracoApiController.cs @@ -1,4 +1,10 @@ -using Umbraco.Core.Composing; +using Umbraco.Core; +using Umbraco.Core.Cache; +using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; namespace Umbraco.Web.WebApi { @@ -6,5 +12,14 @@ namespace Umbraco.Web.WebApi /// Provides a base class for auto-routed Umbraco API controllers. /// public abstract class UmbracoApiController : UmbracoApiControllerBase, IDiscoverable - { } + { + protected UmbracoApiController() + { + } + + protected UmbracoApiController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger, IRuntimeState runtimeState) + : base(globalSettings, umbracoContext, sqlContext, services, applicationCache, logger, profilingLogger, runtimeState) + { + } + } } diff --git a/src/Umbraco.Web/WebApi/UmbracoApiControllerBase.cs b/src/Umbraco.Web/WebApi/UmbracoApiControllerBase.cs index eff5a9cee7..45dedea4ab 100644 --- a/src/Umbraco.Web/WebApi/UmbracoApiControllerBase.cs +++ b/src/Umbraco.Web/WebApi/UmbracoApiControllerBase.cs @@ -1,10 +1,10 @@ using System; using System.Web; using System.Web.Http; -using LightInject; using Microsoft.Owin; using Umbraco.Core; using Umbraco.Core.Cache; +using Umbraco.Core.Composing; using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Persistence; @@ -36,50 +36,42 @@ namespace Umbraco.Web.WebApi /// /// Gets or sets the Umbraco context. /// - [Inject] - public virtual IGlobalSettings GlobalSettings { get; set; } + public virtual IGlobalSettings GlobalSettings { get; } /// /// Gets or sets the Umbraco context. /// - [Inject] - public virtual UmbracoContext UmbracoContext { get; set; } + public virtual UmbracoContext UmbracoContext { get; } /// /// Gets or sets the sql context. /// - [Inject] - public ISqlContext SqlContext { get; set; } + public ISqlContext SqlContext { get; } /// /// Gets or sets the services context. /// - [Inject] - public ServiceContext Services { get; set; } + public ServiceContext Services { get; } /// /// Gets or sets the application cache. /// - [Inject] - public CacheHelper ApplicationCache { get; set; } + public CacheHelper ApplicationCache { get; } /// /// Gets or sets the logger. /// - [Inject] - public ILogger Logger { get; set; } + public ILogger Logger { get; } /// /// Gets or sets the profiling logger. /// - [Inject] - public ProfilingLogger ProfilingLogger { get; set; } + public ProfilingLogger ProfilingLogger { get; } /// /// Gets or sets the runtime state. /// - [Inject] - internal IRuntimeState RuntimeState { get; set; } + internal IRuntimeState RuntimeState { get; } /// /// Gets the application url. @@ -102,6 +94,33 @@ namespace Umbraco.Web.WebApi /// public WebSecurity Security => UmbracoContext.Security; + protected UmbracoApiControllerBase() + : this( + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance() + ) + { + } + + // fixme - Inject fewer things? (Aggregate more) + protected UmbracoApiControllerBase(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger, IRuntimeState runtimeState) + { + GlobalSettings = globalSettings; + UmbracoContext = umbracoContext; + SqlContext = sqlContext; + Services = services; + ApplicationCache = applicationCache; + Logger = logger; + ProfilingLogger = profilingLogger; + RuntimeState = runtimeState; + } + /// /// Tries to get the current HttpContext. /// diff --git a/src/Umbraco.Web/WebApi/UmbracoAuthorizedApiController.cs b/src/Umbraco.Web/WebApi/UmbracoAuthorizedApiController.cs index ad217b43fd..5d60200ed0 100644 --- a/src/Umbraco.Web/WebApi/UmbracoAuthorizedApiController.cs +++ b/src/Umbraco.Web/WebApi/UmbracoAuthorizedApiController.cs @@ -1,6 +1,12 @@ -using Umbraco.Web.WebApi.Filters; +using Umbraco.Core; +using Umbraco.Core.Cache; +using Umbraco.Core.Configuration; +using Umbraco.Core.Logging; +using Umbraco.Web.WebApi.Filters; using Umbraco.Core.Models.Identity; +using Umbraco.Core.Persistence; using Umbraco.Core.Security; +using Umbraco.Core.Services; namespace Umbraco.Web.WebApi { @@ -25,5 +31,14 @@ namespace Umbraco.Web.WebApi protected BackOfficeUserManager UserManager => _userManager ?? (_userManager = TryGetOwinContext().Result.GetBackOfficeUserManager()); + + protected UmbracoAuthorizedApiController() + { + } + + protected UmbracoAuthorizedApiController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger, IRuntimeState runtimeState) + : base(globalSettings, umbracoContext, sqlContext, services, applicationCache, logger, profilingLogger, runtimeState) + { + } } } diff --git a/src/Umbraco.Web/WebServices/TagsController.cs b/src/Umbraco.Web/WebServices/TagsController.cs index 6241cfacf0..786bac2693 100644 --- a/src/Umbraco.Web/WebServices/TagsController.cs +++ b/src/Umbraco.Web/WebServices/TagsController.cs @@ -1,4 +1,10 @@ using System.Collections.Generic; +using Umbraco.Core; +using Umbraco.Core.Cache; +using Umbraco.Core.Configuration; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; using Umbraco.Web.Models; using Umbraco.Web.WebApi; @@ -13,6 +19,15 @@ namespace Umbraco.Web.WebServices /// public class TagsController : UmbracoApiController { + public TagsController() + { + } + + public TagsController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger, IRuntimeState runtimeState) + : base(globalSettings, umbracoContext, sqlContext, services, applicationCache, logger, profilingLogger, runtimeState) + { + } + /// /// Get every tag stored in the database (with optional group) /// From ea9ab66ba7bc49a319312f3184d8e0f424704c5e Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Tue, 19 Jun 2018 18:59:57 +0200 Subject: [PATCH 07/15] Removed an extra using LI. :) --- src/Umbraco.Web/Editors/DataTypeValidateAttribute.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Umbraco.Web/Editors/DataTypeValidateAttribute.cs b/src/Umbraco.Web/Editors/DataTypeValidateAttribute.cs index 43093f563c..91bde1801c 100644 --- a/src/Umbraco.Web/Editors/DataTypeValidateAttribute.cs +++ b/src/Umbraco.Web/Editors/DataTypeValidateAttribute.cs @@ -13,8 +13,6 @@ using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.WebApi; -using LightInject; - namespace Umbraco.Web.Editors { /// From 7af04b0a2fc0f5b21d7f19a4321dfe606e86e550 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Tue, 19 Jun 2018 19:59:12 +0200 Subject: [PATCH 08/15] Tests pass again --- src/Umbraco.Core/Composing/IContainer.cs | 1 + .../Composing/LightInject/ContainerAdapter.cs | 5 +++++ .../Routing/RenderRouteHandlerTests.cs | 8 +++++++ .../Stubs/TestControllerFactory.cs | 3 ++- ...RenderIndexActionSelectorAttributeTests.cs | 7 ++++-- .../Web/Mvc/SurfaceControllerTests.cs | 22 ++++++++++++++----- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/Umbraco.Core/Composing/IContainer.cs b/src/Umbraco.Core/Composing/IContainer.cs index 4e9d8df3c5..a1206c5816 100644 --- a/src/Umbraco.Core/Composing/IContainer.cs +++ b/src/Umbraco.Core/Composing/IContainer.cs @@ -10,6 +10,7 @@ namespace Umbraco.Core.Composing { T TryGetInstance(); T GetInstance(); + object GetInstance(Type parameterType); object ConcreteContainer { get; } void RegisterSingleton(Func factory); void Register(Func factory); diff --git a/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs b/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs index 62e07683a3..906b66bcb1 100644 --- a/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs +++ b/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs @@ -42,5 +42,10 @@ namespace Umbraco.Core.Composing.LightInject { return container.GetInstance(); } + + public object GetInstance(Type type) + { + return container.GetInstance(type); + } } } diff --git a/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs b/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs index c64c70e65a..7e32921363 100644 --- a/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs +++ b/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs @@ -6,6 +6,7 @@ using LightInject; using Moq; using NUnit.Framework; using Umbraco.Core; +using Umbraco.Core.Cache; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers; @@ -17,7 +18,10 @@ using Umbraco.Web.Routing; using Umbraco.Web.WebApi; using Umbraco.Core.Strings; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; using Umbraco.Tests.PublishedContent; using Umbraco.Tests.Testing; using Umbraco.Tests.Testing.Objects.Accessors; @@ -182,6 +186,10 @@ namespace Umbraco.Tests.Routing /// public class CustomDocumentController : RenderMvcController { + public CustomDocumentController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, CacheHelper applicationCache, ILogger logger, ProfilingLogger profilingLogger) : base(globalSettings, umbracoContext, databaseFactory, services, applicationCache, logger, profilingLogger) + { + } + public ActionResult HomePage(ContentModel model) { return View(); diff --git a/src/Umbraco.Tests/TestHelpers/Stubs/TestControllerFactory.cs b/src/Umbraco.Tests/TestHelpers/Stubs/TestControllerFactory.cs index 5e2ce24863..23dc0d4347 100644 --- a/src/Umbraco.Tests/TestHelpers/Stubs/TestControllerFactory.cs +++ b/src/Umbraco.Tests/TestHelpers/Stubs/TestControllerFactory.cs @@ -47,7 +47,8 @@ namespace Umbraco.Tests.TestHelpers.Stubs var allParams = ctor.GetParameters().ToArray(); foreach (var parameter in allParams) { - var found = possibleParams.SingleOrDefault(x => x.GetType() == parameter.ParameterType); + var found = possibleParams.SingleOrDefault(x => x.GetType() == parameter.ParameterType) + ?? Current.Container.GetInstance(parameter.ParameterType); if (found != null) args.Add(found); } if (args.Count == allParams.Length) diff --git a/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs b/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs index f4c15f7c19..bf29f7e35e 100644 --- a/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs @@ -9,6 +9,7 @@ using Moq; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Cache; +using Umbraco.Core.Composing; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Logging; using Umbraco.Core.Profiling; @@ -17,12 +18,12 @@ using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Tests.Testing.Objects.Accessors; using Umbraco.Web; -using Umbraco.Web.Composing; using Umbraco.Web.Models; using Umbraco.Web.Mvc; using Umbraco.Web.PublishedCache; using Umbraco.Web.Routing; using Umbraco.Web.Security; +using Current = Umbraco.Web.Composing.Current; namespace Umbraco.Tests.Web.Mvc { @@ -33,6 +34,7 @@ namespace Umbraco.Tests.Web.Mvc public void SetUp() { Current.UmbracoContextAccessor = new TestUmbracoContextAccessor(); + Core.Composing.Current.Container = Mock.Of(); } [TearDown] @@ -156,7 +158,8 @@ namespace Umbraco.Tests.Web.Mvc } public class MatchesDefaultIndexController : RenderMvcController - { } + { + } public class MatchesOverriddenIndexController : RenderMvcController { diff --git a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs index 4186c56636..f9cff5af9b 100644 --- a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs @@ -7,6 +7,7 @@ using System.Web.Security; using Moq; using NUnit.Framework; using Umbraco.Core.Cache; +using Umbraco.Core.Composing; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Dictionary; using Umbraco.Core.Models.PublishedContent; @@ -16,11 +17,11 @@ using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Tests.Testing; using Umbraco.Tests.Testing.Objects.Accessors; using Umbraco.Web; -using Umbraco.Web.Composing; using Umbraco.Web.Mvc; using Umbraco.Web.PublishedCache; using Umbraco.Web.Routing; using Umbraco.Web.Security; +using Current = Umbraco.Web.Composing.Current; namespace Umbraco.Tests.Web.Mvc { @@ -49,7 +50,7 @@ namespace Umbraco.Tests.Web.Mvc new TestVariationContextAccessor(), true); - var ctrl = new TestSurfaceController { UmbracoContext = umbracoContext }; + var ctrl = new TestSurfaceController(umbracoContext); var result = ctrl.Index(); @@ -71,7 +72,7 @@ namespace Umbraco.Tests.Web.Mvc new TestVariationContextAccessor(), true); - var ctrl = new TestSurfaceController { UmbracoContext = umbCtx }; + var ctrl = new TestSurfaceController(umbCtx); Assert.IsNotNull(ctrl.UmbracoContext); } @@ -91,7 +92,7 @@ namespace Umbraco.Tests.Web.Mvc new TestVariationContextAccessor(), true); - var controller = new TestSurfaceController { UmbracoContext = umbracoContext }; + var controller = new TestSurfaceController(umbracoContext); Container.Register(_ => umbracoContext); Container.InjectProperties(controller); @@ -132,7 +133,7 @@ namespace Umbraco.Tests.Web.Mvc new ServiceContext(), CacheHelper.CreateDisabledCacheHelper()); - var ctrl = new TestSurfaceController { UmbracoContext = umbracoContext, Umbraco = helper }; + var ctrl = new TestSurfaceController(umbracoContext, helper); var result = ctrl.GetContent(2) as PublishedContentResult; Assert.IsNotNull(result); @@ -171,7 +172,7 @@ namespace Umbraco.Tests.Web.Mvc var routeData = new RouteData(); routeData.DataTokens.Add(Core.Constants.Web.UmbracoRouteDefinitionDataToken, routeDefinition); - var ctrl = new TestSurfaceController { UmbracoContext = umbracoContext, Umbraco = new UmbracoHelper() }; + var ctrl = new TestSurfaceController(umbracoContext, new UmbracoHelper()); ctrl.ControllerContext = new ControllerContext(contextBase, routeData, ctrl); var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult; @@ -181,6 +182,15 @@ namespace Umbraco.Tests.Web.Mvc public class TestSurfaceController : SurfaceController { + public TestSurfaceController(UmbracoContext ctx, UmbracoHelper helper = null) + : base(ctx, null, new ServiceContext(), Mock.Of(), null, null) + { + if (helper != null) + { + Umbraco = helper; + } + } + public ActionResult Index() { return View(); From db2ad8778455fbb3a06fdef30aff0459485dc2bd Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Wed, 20 Jun 2018 19:57:42 +0200 Subject: [PATCH 09/15] Removed named service reference from repo ctors. Now registered with factory method that looks up services by name instead. --- .../RepositoryCompositionRoot.cs | 29 +++++++++++++------ src/Umbraco.Core/IO/FileSystems.cs | 3 ++ .../Repositories/Implement/AuditRepository.cs | 4 +-- .../Implement/PartialViewMacroRepository.cs | 5 ++-- .../Implement/PartialViewRepository.cs | 3 +- .../Implement/RelationRepository.cs | 4 +-- .../Implement/ScriptRepository.cs | 3 +- .../Implement/StylesheetRepository.cs | 3 +- .../Repositories/Implement/TaskRepository.cs | 4 +-- .../Implement/TaskTypeRepository.cs | 4 +-- .../Implement/TemplateRepository.cs | 5 ++-- src/Umbraco.Web/Mvc/PluginController.cs | 12 ++++---- 12 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/Umbraco.Core/Composing/CompositionRoots/RepositoryCompositionRoot.cs b/src/Umbraco.Core/Composing/CompositionRoots/RepositoryCompositionRoot.cs index 26a95af801..2323da165a 100644 --- a/src/Umbraco.Core/Composing/CompositionRoots/RepositoryCompositionRoot.cs +++ b/src/Umbraco.Core/Composing/CompositionRoots/RepositoryCompositionRoot.cs @@ -1,8 +1,12 @@ using System; using LightInject; using Umbraco.Core.Cache; +using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.IO; +using Umbraco.Core.Logging; using Umbraco.Core.Persistence.Repositories; using Umbraco.Core.Persistence.Repositories.Implement; +using Umbraco.Core.Scoping; namespace Umbraco.Core.Composing.CompositionRoots { @@ -36,7 +40,7 @@ namespace Umbraco.Core.Composing.CompositionRoots // some repositories have an annotated ctor parameter to pick the right cache helper // repositories - container.RegisterSingleton(); + container.RegisterSingleton(f => new AuditRepository(f.GetInstance(), f.GetInstance(DisabledCache), f.GetInstance())); container.RegisterSingleton(); container.RegisterSingleton(); container.RegisterSingleton(); @@ -59,23 +63,30 @@ namespace Umbraco.Core.Composing.CompositionRoots container.RegisterSingleton(); container.RegisterSingleton(); container.RegisterSingleton(); - container.RegisterSingleton(); + container.RegisterSingleton(f => new RelationRepository(f.GetInstance(), f.GetInstance(DisabledCache), f.GetInstance(), f.GetInstance())); container.RegisterSingleton(); container.RegisterSingleton(); container.RegisterSingleton(); - container.RegisterSingleton(); - container.RegisterSingleton(); - container.RegisterSingleton(); + container.RegisterSingleton(f => new TaskRepository(f.GetInstance(), f.GetInstance(DisabledCache), f.GetInstance())); + container.RegisterSingleton(f => new TaskTypeRepository(f.GetInstance(), f.GetInstance(DisabledCache), f.GetInstance())); + container.RegisterSingleton(f => new TemplateRepository( + f.GetInstance(), + f.GetInstance(), + f.GetInstance(), + f.GetInstance(), + f.GetInstance(Constants.Composing.FileSystems.MasterpageFileSystem), + f.GetInstance(Constants.Composing.FileSystems.ViewFileSystem) + )); container.RegisterSingleton(); container.RegisterSingleton(); container.RegisterSingleton(); // repositories that depend on a filesystem // these have an annotated ctor parameter to pick the right file system - container.RegisterSingleton(); - container.RegisterSingleton(); - container.RegisterSingleton(); - container.RegisterSingleton(); + container.RegisterSingleton(f => new PartialViewMacroRepository(f.GetInstance("PartialViewMacroFileSystem"))); + container.RegisterSingleton(f => new PartialViewRepository(f.GetInstance("PartialViewFileSystem"))); + container.RegisterSingleton(f => new ScriptRepository(f.GetInstance("ScriptFileSystem"), f.GetInstance())); + container.RegisterSingleton(f => new StylesheetRepository(f.GetInstance("StylesheetFileSystem"))); } } } diff --git a/src/Umbraco.Core/IO/FileSystems.cs b/src/Umbraco.Core/IO/FileSystems.cs index 5d7088b0e1..2fb5941b17 100644 --- a/src/Umbraco.Core/IO/FileSystems.cs +++ b/src/Umbraco.Core/IO/FileSystems.cs @@ -302,6 +302,9 @@ namespace Umbraco.Core.IO // could be optimized by having FileSystemWrapper inherit from ShadowWrapper, maybe var innerFs = GetUnderlyingFileSystemNoCache(alias, fallback); var shadowWrapper = new ShadowWrapper(innerFs, "typed/" + alias, () => IsScoped()); + + // fixme - switch to using container. where are these registered? + var fs = (IFileSystem) Activator.CreateInstance(typeof(TFileSystem), shadowWrapper); _wrappers.Add(shadowWrapper); // keeping a reference to the wrapper return fs; diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/AuditRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/AuditRepository.cs index e171077bb7..d643e1f5a5 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/AuditRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/AuditRepository.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; -using LightInject; using NPoco; using Umbraco.Core.Cache; -using Umbraco.Core.Composing.CompositionRoots; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Persistence.DatabaseModelDefinitions; @@ -16,7 +14,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement { internal class AuditRepository : NPocoRepositoryBase, IAuditRepository { - public AuditRepository(IScopeAccessor scopeAccessor, [Inject(RepositoryCompositionRoot.DisabledCache)] CacheHelper cache, ILogger logger) + public AuditRepository(IScopeAccessor scopeAccessor, CacheHelper cache, ILogger logger) : base(scopeAccessor, cache, logger) { } diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/PartialViewMacroRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/PartialViewMacroRepository.cs index 741bb98e7c..879766fc81 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/PartialViewMacroRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/PartialViewMacroRepository.cs @@ -1,5 +1,4 @@ -using LightInject; -using Umbraco.Core.IO; +using Umbraco.Core.IO; using Umbraco.Core.Models; namespace Umbraco.Core.Persistence.Repositories.Implement @@ -7,7 +6,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement internal class PartialViewMacroRepository : PartialViewRepository, IPartialViewMacroRepository { - public PartialViewMacroRepository([Inject("PartialViewMacroFileSystem")] IFileSystem fileSystem) + public PartialViewMacroRepository(IFileSystem fileSystem) : base(fileSystem) { } diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/PartialViewRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/PartialViewRepository.cs index 2aa63813e5..a996251927 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/PartialViewRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/PartialViewRepository.cs @@ -2,7 +2,6 @@ using System.IO; using System.Linq; using System.Text; -using LightInject; using Umbraco.Core.IO; using Umbraco.Core.Models; @@ -10,7 +9,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement { internal class PartialViewRepository : FileRepository, IPartialViewRepository { - public PartialViewRepository([Inject("PartialViewFileSystem")] IFileSystem fileSystem) + public PartialViewRepository(IFileSystem fileSystem) : base(fileSystem) { } diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs index f79344028f..bb153beda9 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; -using LightInject; using NPoco; using Umbraco.Core.Cache; -using Umbraco.Core.Composing.CompositionRoots; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.Entities; @@ -22,7 +20,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement { private readonly IRelationTypeRepository _relationTypeRepository; - public RelationRepository(IScopeAccessor scopeAccessor, [Inject(RepositoryCompositionRoot.DisabledCache)] CacheHelper cache, ILogger logger, IRelationTypeRepository relationTypeRepository) + public RelationRepository(IScopeAccessor scopeAccessor, CacheHelper cache, ILogger logger, IRelationTypeRepository relationTypeRepository) : base(scopeAccessor, cache, logger) { _relationTypeRepository = relationTypeRepository; diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/ScriptRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/ScriptRepository.cs index d5719554c9..735a1aa74f 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/ScriptRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/ScriptRepository.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using LightInject; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.IO; using Umbraco.Core.Models; @@ -16,7 +15,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement { private readonly IContentSection _contentConfig; - public ScriptRepository([Inject("ScriptFileSystem")] IFileSystem fileSystem, IContentSection contentConfig) + public ScriptRepository(IFileSystem fileSystem, IContentSection contentConfig) : base(fileSystem) { _contentConfig = contentConfig ?? throw new ArgumentNullException(nameof(contentConfig)); diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/StylesheetRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/StylesheetRepository.cs index 73dcb44fef..8139f4948e 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/StylesheetRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/StylesheetRepository.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using LightInject; using Umbraco.Core.IO; using Umbraco.Core.Models; @@ -12,7 +11,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement /// internal class StylesheetRepository : FileRepository, IStylesheetRepository { - public StylesheetRepository([Inject("StylesheetFileSystem")] IFileSystem fileSystem) + public StylesheetRepository(IFileSystem fileSystem) : base(fileSystem) { } diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/TaskRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/TaskRepository.cs index f33a1c852a..fd79c14c93 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/TaskRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/TaskRepository.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; -using LightInject; using NPoco; using Umbraco.Core.Cache; -using Umbraco.Core.Composing.CompositionRoots; using Umbraco.Core.Exceptions; using Umbraco.Core.Logging; using Umbraco.Core.Models; @@ -17,7 +15,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement { internal class TaskRepository : NPocoRepositoryBase, ITaskRepository { - public TaskRepository(IScopeAccessor scopeAccessor, [Inject(RepositoryCompositionRoot.DisabledCache)] CacheHelper cache, ILogger logger) + public TaskRepository(IScopeAccessor scopeAccessor, CacheHelper cache, ILogger logger) : base(scopeAccessor, cache, logger) { } diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/TaskTypeRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/TaskTypeRepository.cs index abd21002a1..465474a644 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/TaskTypeRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/TaskTypeRepository.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; -using LightInject; using NPoco; using Umbraco.Core.Cache; -using Umbraco.Core.Composing.CompositionRoots; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Persistence.Dtos; @@ -16,7 +14,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement { internal class TaskTypeRepository : NPocoRepositoryBase, ITaskTypeRepository { - public TaskTypeRepository(IScopeAccessor scopeAccessor, [Inject(RepositoryCompositionRoot.DisabledCache)] CacheHelper cache, ILogger logger) + public TaskTypeRepository(IScopeAccessor scopeAccessor, CacheHelper cache, ILogger logger) : base(scopeAccessor, cache, logger) { } diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/TemplateRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/TemplateRepository.cs index 32080c4a12..4afc1bfbdb 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/TemplateRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/TemplateRepository.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; -using LightInject; using NPoco; using Umbraco.Core.Cache; using Umbraco.Core.Configuration.UmbracoSettings; @@ -31,8 +30,8 @@ namespace Umbraco.Core.Persistence.Repositories.Implement private readonly MasterPageHelper _masterPageHelper; public TemplateRepository(IScopeAccessor scopeAccessor, CacheHelper cache, ILogger logger, ITemplatesSection templateConfig, - [Inject(Constants.Composing.FileSystems.MasterpageFileSystem)] IFileSystem masterpageFileSystem, - [Inject(Constants.Composing.FileSystems.ViewFileSystem)] IFileSystem viewFileSystem) + IFileSystem masterpageFileSystem, + IFileSystem viewFileSystem) : base(scopeAccessor, cache, logger) { _masterpagesFileSystem = masterpageFileSystem; diff --git a/src/Umbraco.Web/Mvc/PluginController.cs b/src/Umbraco.Web/Mvc/PluginController.cs index 6e44cca112..2e28e7fe2c 100644 --- a/src/Umbraco.Web/Mvc/PluginController.cs +++ b/src/Umbraco.Web/Mvc/PluginController.cs @@ -35,32 +35,32 @@ namespace Umbraco.Web.Mvc /// /// Gets or sets the Umbraco context. /// - public virtual UmbracoContext UmbracoContext { get; set; } + public virtual UmbracoContext UmbracoContext { get; } /// /// Gets or sets the database context. /// - public IUmbracoDatabaseFactory DatabaseFactory { get; set; } + public IUmbracoDatabaseFactory DatabaseFactory { get; } /// /// Gets or sets the services context. /// - public ServiceContext Services { get; set; } + public ServiceContext Services { get; } /// /// Gets or sets the application cache. /// - public CacheHelper ApplicationCache { get; set; } + public CacheHelper ApplicationCache { get; } /// /// Gets or sets the logger. /// - public ILogger Logger { get; set; } + public ILogger Logger { get; } /// /// Gets or sets the profiling logger. /// - public ProfilingLogger ProfilingLogger { get; set; } + public ProfilingLogger ProfilingLogger { get; } /// /// Gets the membership helper. From 590765e9c637be8756e8dd39192a6ed1714a5151 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Wed, 20 Jun 2018 20:00:34 +0200 Subject: [PATCH 10/15] Tests build >< --- .../Web/Controllers/PluginControllerAreaTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Tests/Web/Controllers/PluginControllerAreaTests.cs b/src/Umbraco.Tests/Web/Controllers/PluginControllerAreaTests.cs index 669c607aea..01525f12da 100644 --- a/src/Umbraco.Tests/Web/Controllers/PluginControllerAreaTests.cs +++ b/src/Umbraco.Tests/Web/Controllers/PluginControllerAreaTests.cs @@ -54,8 +54,8 @@ namespace Umbraco.Tests.Web.Controllers public class Plugin1Controller : PluginController { public Plugin1Controller(UmbracoContext umbracoContext) + : base(umbracoContext, null, null, null, null, null) { - UmbracoContext = umbracoContext; } } @@ -63,8 +63,8 @@ namespace Umbraco.Tests.Web.Controllers public class Plugin2Controller : PluginController { public Plugin2Controller(UmbracoContext umbracoContext) + : base(umbracoContext, null, null, null, null, null) { - UmbracoContext = umbracoContext; } } @@ -72,16 +72,16 @@ namespace Umbraco.Tests.Web.Controllers public class Plugin3Controller : PluginController { public Plugin3Controller(UmbracoContext umbracoContext) + : base(umbracoContext, null, null, null, null, null) { - UmbracoContext = umbracoContext; } } public class Plugin4Controller : PluginController { public Plugin4Controller(UmbracoContext umbracoContext) + : base(umbracoContext, null, null, null, null, null) { - UmbracoContext = umbracoContext; } } From 34390dec12b38368bd402f19e4430748e47b97b9 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Wed, 20 Jun 2018 22:00:41 +0200 Subject: [PATCH 11/15] Almost got MediaFileSystem in container. --- src/Umbraco.Core/Composing/IContainer.cs | 1 + src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs | 5 +++++ src/Umbraco.Core/IO/FileSystems.cs | 3 ++- src/Umbraco.Tests/IO/FileSystemsTests.cs | 3 +++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Core/Composing/IContainer.cs b/src/Umbraco.Core/Composing/IContainer.cs index a1206c5816..6583509d58 100644 --- a/src/Umbraco.Core/Composing/IContainer.cs +++ b/src/Umbraco.Core/Composing/IContainer.cs @@ -15,5 +15,6 @@ namespace Umbraco.Core.Composing void RegisterSingleton(Func factory); void Register(Func factory); T RegisterCollectionBuilder(); + T GetInstance(params object[] args); } } diff --git a/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs b/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs index 906b66bcb1..42a04b9a15 100644 --- a/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs +++ b/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs @@ -43,6 +43,11 @@ namespace Umbraco.Core.Composing.LightInject return container.GetInstance(); } + public T GetInstance(params object[] args) + { + return (T)container.GetInstance(typeof(T), args); + } + public object GetInstance(Type type) { return container.GetInstance(type); diff --git a/src/Umbraco.Core/IO/FileSystems.cs b/src/Umbraco.Core/IO/FileSystems.cs index 2fb5941b17..9749078923 100644 --- a/src/Umbraco.Core/IO/FileSystems.cs +++ b/src/Umbraco.Core/IO/FileSystems.cs @@ -305,7 +305,8 @@ namespace Umbraco.Core.IO // fixme - switch to using container. where are these registered? - var fs = (IFileSystem) Activator.CreateInstance(typeof(TFileSystem), shadowWrapper); + //var fs = (IFileSystem) Activator.CreateInstance(typeof(TFileSystem), shadowWrapper); + var fs = Current.Container.GetInstance((IFileSystem)shadowWrapper); _wrappers.Add(shadowWrapper); // keeping a reference to the wrapper return fs; }); diff --git a/src/Umbraco.Tests/IO/FileSystemsTests.cs b/src/Umbraco.Tests/IO/FileSystemsTests.cs index 79b1d8bf6c..83792b9a0b 100644 --- a/src/Umbraco.Tests/IO/FileSystemsTests.cs +++ b/src/Umbraco.Tests/IO/FileSystemsTests.cs @@ -33,6 +33,9 @@ namespace Umbraco.Tests.IO _container.Register(_ => Mock.Of()); _container.Register(_ => Mock.Of()); + _container.Register(); + _container.Register(); + // make sure we start clean // because some tests will create corrupt or weird filesystems FileSystems.Reset(); From 3a7d7e370e8616a132443a0491edc3531db67aa9 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Tue, 26 Jun 2018 00:26:57 +0200 Subject: [PATCH 12/15] MediaFileSystem now purely injected. A bit messy, and probably leaves quite a bit of dead / useless code around that should be deleted. Needs some manual testing. --- src/Umbraco.Core/IO/FileSystems.cs | 2 +- src/Umbraco.Core/IO/MediaFileSystem.cs | 16 ++++++++-------- src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs | 3 +++ src/Umbraco.Tests/IO/FileSystemsTests.cs | 4 ++-- src/Umbraco.Tests/Models/ContentTests.cs | 8 +++++--- src/Umbraco.Tests/Models/MediaXmlTest.cs | 2 +- .../PropertyEditors/ImageCropperTest.cs | 2 +- src/Umbraco.Tests/TestHelpers/TestObjects.cs | 3 ++- src/Umbraco.Tests/Testing/UmbracoTestBase.cs | 3 ++- 9 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Umbraco.Core/IO/FileSystems.cs b/src/Umbraco.Core/IO/FileSystems.cs index 9749078923..061413d070 100644 --- a/src/Umbraco.Core/IO/FileSystems.cs +++ b/src/Umbraco.Core/IO/FileSystems.cs @@ -318,7 +318,7 @@ namespace Umbraco.Core.IO // validate the ctor var constructor = fsType.GetConstructors().SingleOrDefault(x - => x.GetParameters().Length == 1 && TypeHelper.IsTypeAssignableFrom(x.GetParameters().Single().ParameterType)); + => x.GetParameters().Length >= 1 && TypeHelper.IsTypeAssignableFrom(x.GetParameters().First().ParameterType)); if (constructor == null) throw new InvalidOperationException("Type " + fsType.FullName + " must inherit from FileSystemWrapper and have a constructor that accepts one parameter of type " + typeof(IFileSystem).FullName + "."); diff --git a/src/Umbraco.Core/IO/MediaFileSystem.cs b/src/Umbraco.Core/IO/MediaFileSystem.cs index b3fc2ccb61..b59d5b6924 100644 --- a/src/Umbraco.Core/IO/MediaFileSystem.cs +++ b/src/Umbraco.Core/IO/MediaFileSystem.cs @@ -6,10 +6,8 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; -using LightInject; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; -using Umbraco.Core.Composing; using Umbraco.Core.Exceptions; using Umbraco.Core.Logging; using Umbraco.Core.Media; @@ -35,21 +33,23 @@ namespace Umbraco.Core.IO // { 500, "big-thumb" } //}; - public MediaFileSystem(IFileSystem wrapped) + public MediaFileSystem(IFileSystem wrapped, IContentSection contentConfig, ILogger logger) : base(wrapped) { + ContentConfig = contentConfig; + Logger = logger; + + // fixme - remove obsolete comments // 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 - ((IServiceContainer)Current.Container.ConcreteContainer).InjectProperties(this); + //((IServiceContainer)Current.Container.ConcreteContainer).InjectProperties(this); UploadAutoFillProperties = new UploadAutoFillProperties(this, Logger, ContentConfig); } - [Inject] - internal IContentSection ContentConfig { get; set; } + internal IContentSection ContentConfig { get; } - [Inject] - internal ILogger Logger { get; set; } + internal ILogger Logger { get; } internal UploadAutoFillProperties UploadAutoFillProperties { get; } diff --git a/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs b/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs index a90d5b5b4c..a9a1369a97 100644 --- a/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs +++ b/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs @@ -44,6 +44,9 @@ namespace Umbraco.Core.Runtime composition.Container.Register(); // register filesystems + + composition.Container.Register((f, wrappedFileSystem) => new MediaFileSystem(wrappedFileSystem, f.GetInstance(), f.GetInstance())); + composition.Container.RegisterSingleton(); composition.Container.RegisterSingleton(factory => factory.GetInstance().MediaFileSystem); composition.Container.RegisterSingleton(factory => factory.GetInstance().ScriptsFileSystem, Constants.Composing.FileSystems.ScriptFileSystem); diff --git a/src/Umbraco.Tests/IO/FileSystemsTests.cs b/src/Umbraco.Tests/IO/FileSystemsTests.cs index 83792b9a0b..c0f13c6e1b 100644 --- a/src/Umbraco.Tests/IO/FileSystemsTests.cs +++ b/src/Umbraco.Tests/IO/FileSystemsTests.cs @@ -33,8 +33,8 @@ namespace Umbraco.Tests.IO _container.Register(_ => Mock.Of()); _container.Register(_ => Mock.Of()); - _container.Register(); - _container.Register(); + _container.Register((f, x) => new MediaFileSystem(x, f.GetInstance(), f.GetInstance())); + _container.Register((f, x) => new NonConfiguredTypeFileSystem(x)); // make sure we start clean // because some tests will create corrupt or weird filesystems diff --git a/src/Umbraco.Tests/Models/ContentTests.cs b/src/Umbraco.Tests/Models/ContentTests.cs index 2e68873f77..41228eb89e 100644 --- a/src/Umbraco.Tests/Models/ContentTests.cs +++ b/src/Umbraco.Tests/Models/ContentTests.cs @@ -5,7 +5,8 @@ using System.Globalization; using System.IO; using System.Linq; using System.Web; -using Moq; +using Moq; +using LightInject; using NUnit.Framework; using Umbraco.Core.Cache; using Umbraco.Core.Configuration.UmbracoSettings; @@ -22,7 +23,7 @@ using Umbraco.Tests.Testing; namespace Umbraco.Tests.Models { - [TestFixture] + [TestFixture] public class ContentTests : UmbracoTestBase { public override void SetUp() @@ -36,8 +37,9 @@ namespace Umbraco.Tests.Models protected override void Compose() { base.Compose(); - + Container.Register(_ => Mock.Of()); + Container.Register((factory, fileSystem) => new MediaFileSystem(fileSystem, factory.GetInstance(), factory.GetInstance())); Container.Register(); Container.Register(_ => Mock.Of()); Container.Register(_ => Mock.Of()); diff --git a/src/Umbraco.Tests/Models/MediaXmlTest.cs b/src/Umbraco.Tests/Models/MediaXmlTest.cs index 6f28842f18..1f4eaf9d8a 100644 --- a/src/Umbraco.Tests/Models/MediaXmlTest.cs +++ b/src/Umbraco.Tests/Models/MediaXmlTest.cs @@ -29,7 +29,7 @@ namespace Umbraco.Tests.Models // reference, so static ctor runs, so event handlers register // and then, this will reset the width, height... because the file does not exist, of course ;-( - var ignored = new FileUploadPropertyEditor(Mock.Of(), new MediaFileSystem(Mock.Of())); + var ignored = new FileUploadPropertyEditor(Mock.Of(), new MediaFileSystem(Mock.Of(), Mock.Of(), Mock.Of())); var media = MockedMedia.CreateMediaImage(mediaType, -1); media.WriterId = -1; // else it's zero and that's not a user and it breaks the tests diff --git a/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs b/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs index 83d5fea1d1..8df585fb25 100644 --- a/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs +++ b/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs @@ -72,7 +72,7 @@ namespace Umbraco.Tests.PropertyEditors container.Register(f => Mock.Of()); container.Register(f => Mock.Of()); - var mediaFileSystem = new MediaFileSystem(Mock.Of()); + var mediaFileSystem = new MediaFileSystem(Mock.Of(), Mock.Of(), Mock.Of()); var dataTypeService = new TestObjects.TestDataTypeService( new DataType(new ImageCropperPropertyEditor(Mock.Of(), mediaFileSystem, Mock.Of())) { Id = 1 }); diff --git a/src/Umbraco.Tests/TestHelpers/TestObjects.cs b/src/Umbraco.Tests/TestHelpers/TestObjects.cs index 485cce4c0a..7e3d887d77 100644 --- a/src/Umbraco.Tests/TestHelpers/TestObjects.cs +++ b/src/Umbraco.Tests/TestHelpers/TestObjects.cs @@ -10,6 +10,7 @@ using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Composing; using Umbraco.Core.Configuration; +using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Events; using Umbraco.Core.IO; using Umbraco.Core.Logging; @@ -116,7 +117,7 @@ namespace Umbraco.Tests.TestHelpers if (logger == null) throw new ArgumentNullException(nameof(logger)); if (eventMessagesFactory == null) throw new ArgumentNullException(nameof(eventMessagesFactory)); - var mediaFileSystem = new MediaFileSystem(Mock.Of()); + var mediaFileSystem = new MediaFileSystem(Mock.Of(), Mock.Of(), Mock.Of()); var externalLoginService = GetLazyService(container, c => new ExternalLoginService(scopeProvider, logger, eventMessagesFactory, GetRepo(c))); var publicAccessService = GetLazyService(container, c => new PublicAccessService(scopeProvider, logger, eventMessagesFactory, GetRepo(c))); diff --git a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs index b5f3de72a4..2048b7cb58 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs @@ -13,6 +13,7 @@ using Umbraco.Core.Components; using Umbraco.Core.Composing; using Umbraco.Core.Composing.CompositionRoots; using Umbraco.Core.Configuration; +using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Events; using Umbraco.Core.IO; using Umbraco.Core.Logging; @@ -267,7 +268,7 @@ namespace Umbraco.Tests.Testing Container.RegisterSingleton(factory => globalSettings); Container.RegisterSingleton(factory => umbracoSettings.Content); Container.RegisterSingleton(factory => umbracoSettings.Templates); - Container.Register(factory => new MediaFileSystem(Mock.Of())); + Container.Register((factory, fileSystem) => new MediaFileSystem(fileSystem, factory.GetInstance(), factory.GetInstance())); Container.RegisterSingleton(factory => ExamineManager.Instance); // replace some stuff From 49e95146090082b75b53d71f0eddc96c851cfaa7 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Tue, 26 Jun 2018 00:33:38 +0200 Subject: [PATCH 13/15] Added "Slow" category to several fixtures, cutting test time in half for most regression runs. --- src/Umbraco.Tests/Integration/ContentEventsTests.cs | 1 + src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs | 1 + src/Umbraco.Tests/Services/ContentServiceTests.cs | 1 + src/Umbraco.Tests/Services/ContentTypeServiceTests.cs | 1 + src/Umbraco.Tests/Services/Importing/PackageImportTests.cs | 1 + src/Umbraco.Tests/Services/MemberServiceTests.cs | 1 + 6 files changed, 6 insertions(+) diff --git a/src/Umbraco.Tests/Integration/ContentEventsTests.cs b/src/Umbraco.Tests/Integration/ContentEventsTests.cs index 73f95c44a7..c0b66de2a0 100644 --- a/src/Umbraco.Tests/Integration/ContentEventsTests.cs +++ b/src/Umbraco.Tests/Integration/ContentEventsTests.cs @@ -22,6 +22,7 @@ using static Umbraco.Tests.Cache.DistributedCache.DistributedCacheTests; namespace Umbraco.Tests.Integration { [TestFixture] + [Category("Slow")] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class ContentEventsTests : TestWithSomeContentBase { diff --git a/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs b/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs index 38990405c4..66f6e2f3ef 100644 --- a/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs +++ b/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs @@ -13,6 +13,7 @@ namespace Umbraco.Tests.Scheduling { [TestFixture] [Timeout(30000)] + [Category("Slow")] public class BackgroundTaskRunnerTests { private ILogger _logger; diff --git a/src/Umbraco.Tests/Services/ContentServiceTests.cs b/src/Umbraco.Tests/Services/ContentServiceTests.cs index 0326ce0a15..f1a4db949c 100644 --- a/src/Umbraco.Tests/Services/ContentServiceTests.cs +++ b/src/Umbraco.Tests/Services/ContentServiceTests.cs @@ -34,6 +34,7 @@ namespace Umbraco.Tests.Services /// as well as configuration. /// [TestFixture] + [Category("Slow")] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, PublishedRepositoryEvents = true, WithApplication = true)] public class ContentServiceTests : TestWithSomeContentBase { diff --git a/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs b/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs index c9f8ba52ad..8d316b7721 100644 --- a/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs +++ b/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs @@ -19,6 +19,7 @@ using Umbraco.Tests.Testing; namespace Umbraco.Tests.Services { [TestFixture] + [Category("Slow")] [Apartment(ApartmentState.STA)] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, PublishedRepositoryEvents = true)] public class ContentTypeServiceTests : TestWithSomeContentBase diff --git a/src/Umbraco.Tests/Services/Importing/PackageImportTests.cs b/src/Umbraco.Tests/Services/Importing/PackageImportTests.cs index a810d4c644..5e6bb18159 100644 --- a/src/Umbraco.Tests/Services/Importing/PackageImportTests.cs +++ b/src/Umbraco.Tests/Services/Importing/PackageImportTests.cs @@ -16,6 +16,7 @@ using LightInject; namespace Umbraco.Tests.Services.Importing { [TestFixture] + [Category("Slow")] [Apartment(ApartmentState.STA)] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class PackageImportTests : TestWithSomeContentBase diff --git a/src/Umbraco.Tests/Services/MemberServiceTests.cs b/src/Umbraco.Tests/Services/MemberServiceTests.cs index 968b27a4f9..ea6dd8248e 100644 --- a/src/Umbraco.Tests/Services/MemberServiceTests.cs +++ b/src/Umbraco.Tests/Services/MemberServiceTests.cs @@ -24,6 +24,7 @@ using Umbraco.Web.Security.Providers; namespace Umbraco.Tests.Services { [TestFixture] + [Category("Slow")] [Apartment(ApartmentState.STA)] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, PublishedRepositoryEvents = true, WithApplication = true)] public class MemberServiceTests : TestWithSomeContentBase From 3d2810e6b16b16a50c4a9c967f011a4ae661b05f Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Thu, 28 Jun 2018 22:38:14 +0200 Subject: [PATCH 14/15] Fixes constructor dependency conondrum for MediaFileSystem. It is Completely Broken (tm). Need to review whole lifetime & registration of it. --- src/Umbraco.Core/Composing/IContainer.cs | 3 ++- src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs | 7 ++++++- src/Umbraco.Core/IO/FileSystems.cs | 2 +- src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs | 1 + src/Umbraco.Tests/Testing/UmbracoTestBase.cs | 4 ++++ 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Core/Composing/IContainer.cs b/src/Umbraco.Core/Composing/IContainer.cs index 6583509d58..331c1e9141 100644 --- a/src/Umbraco.Core/Composing/IContainer.cs +++ b/src/Umbraco.Core/Composing/IContainer.cs @@ -14,7 +14,8 @@ namespace Umbraco.Core.Composing object ConcreteContainer { get; } void RegisterSingleton(Func factory); void Register(Func factory); + void Register(Func factory); T RegisterCollectionBuilder(); - T GetInstance(params object[] args); + T GetInstance(object[] args); } } diff --git a/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs b/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs index 42a04b9a15..f7ffa68111 100644 --- a/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs +++ b/src/Umbraco.Core/Composing/LightInject/ContainerAdapter.cs @@ -23,6 +23,11 @@ namespace Umbraco.Core.Composing.LightInject container.Register(f => factory(this)); } + public void Register(Func factory) + { + container.Register((f, x) => factory(this, x)); + } + public T RegisterCollectionBuilder() { return container.RegisterCollectionBuilder(); @@ -43,7 +48,7 @@ namespace Umbraco.Core.Composing.LightInject return container.GetInstance(); } - public T GetInstance(params object[] args) + public T GetInstance(object[] args) { return (T)container.GetInstance(typeof(T), args); } diff --git a/src/Umbraco.Core/IO/FileSystems.cs b/src/Umbraco.Core/IO/FileSystems.cs index 061413d070..af6d176794 100644 --- a/src/Umbraco.Core/IO/FileSystems.cs +++ b/src/Umbraco.Core/IO/FileSystems.cs @@ -306,7 +306,7 @@ namespace Umbraco.Core.IO // fixme - switch to using container. where are these registered? //var fs = (IFileSystem) Activator.CreateInstance(typeof(TFileSystem), shadowWrapper); - var fs = Current.Container.GetInstance((IFileSystem)shadowWrapper); + var fs = Current.Container.GetInstance(new object[] { (IFileSystem)shadowWrapper }); _wrappers.Add(shadowWrapper); // keeping a reference to the wrapper return fs; }); diff --git a/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs b/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs index a9a1369a97..1744e8063f 100644 --- a/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs +++ b/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs @@ -46,6 +46,7 @@ namespace Umbraco.Core.Runtime // register filesystems composition.Container.Register((f, wrappedFileSystem) => new MediaFileSystem(wrappedFileSystem, f.GetInstance(), f.GetInstance())); + composition.Container.RegisterConstructorDependency((factory, parameterInfo) => factory.GetInstance().MediaFileSystem); composition.Container.RegisterSingleton(); composition.Container.RegisterSingleton(factory => factory.GetInstance().MediaFileSystem); diff --git a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs index 2048b7cb58..d2a9481251 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs @@ -268,7 +268,11 @@ namespace Umbraco.Tests.Testing Container.RegisterSingleton(factory => globalSettings); Container.RegisterSingleton(factory => umbracoSettings.Content); Container.RegisterSingleton(factory => umbracoSettings.Templates); + + // fixme - The whole MediaFileSystem coupling thing seems broken. Container.Register((factory, fileSystem) => new MediaFileSystem(fileSystem, factory.GetInstance(), factory.GetInstance())); + Container.RegisterConstructorDependency((factory, parameterInfo) => factory.GetInstance().MediaFileSystem); + Container.RegisterSingleton(factory => ExamineManager.Instance); // replace some stuff From 91a5605341d8f3aa7eb6c7320143db534642b6d1 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Thu, 28 Jun 2018 23:28:22 +0200 Subject: [PATCH 15/15] MembershipHelper presumably working without property injection. (But OMG! there's nearly no test coverage of Umbraco.Web :O ) --- .../TestControllerActivatorBase.cs | 3 +- .../Accessors/TestUmbracoContextAccessor.cs | 9 +++ .../Testing/TestingTests/MockTests.cs | 3 +- .../Web/Mvc/SurfaceControllerTests.cs | 3 +- .../CompositionRoots/HelperCompositionRoot.cs | 17 ++++ src/Umbraco.Web/HtmlHelperRenderExtensions.cs | 2 +- src/Umbraco.Web/Models/LoginStatusModel.cs | 2 +- src/Umbraco.Web/Models/ProfileModel.cs | 2 +- src/Umbraco.Web/Models/RegisterModel.cs | 2 +- src/Umbraco.Web/Routing/PublishedRouter.cs | 3 +- .../Runtime/WebRuntimeComponent.cs | 2 + src/Umbraco.Web/Security/MembershipHelper.cs | 81 ++++++++----------- src/Umbraco.Web/Security/WebSecurity.cs | 2 +- .../UI/Controls/UmbracoUserControl.cs | 2 +- src/Umbraco.Web/Umbraco.Web.csproj | 1 + src/Umbraco.Web/UmbracoHelper.cs | 2 +- 16 files changed, 76 insertions(+), 60 deletions(-) create mode 100644 src/Umbraco.Web/Composing/CompositionRoots/HelperCompositionRoot.cs diff --git a/src/Umbraco.Tests/TestHelpers/ControllerTesting/TestControllerActivatorBase.cs b/src/Umbraco.Tests/TestHelpers/ControllerTesting/TestControllerActivatorBase.cs index ded14c40ab..c9f74d689d 100644 --- a/src/Umbraco.Tests/TestHelpers/ControllerTesting/TestControllerActivatorBase.cs +++ b/src/Umbraco.Tests/TestHelpers/ControllerTesting/TestControllerActivatorBase.cs @@ -29,6 +29,7 @@ using Umbraco.Web.Security; using Umbraco.Web.WebApi; using LightInject; using System.Globalization; +using Umbraco.Core.Logging; using Umbraco.Tests.Testing.Objects.Accessors; namespace Umbraco.Tests.TestHelpers.ControllerTesting @@ -157,7 +158,7 @@ namespace Umbraco.Tests.TestHelpers.ControllerTesting urlHelper.Setup(provider => provider.GetUrl(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns("/hello/world/1234"); - var membershipHelper = new MembershipHelper(umbCtx, Mock.Of(), Mock.Of()); + var membershipHelper = new MembershipHelper(new TestUmbracoContextAccessor(umbCtx), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), null); var mockedTypedContent = Mock.Of(); diff --git a/src/Umbraco.Tests/Testing/Objects/Accessors/TestUmbracoContextAccessor.cs b/src/Umbraco.Tests/Testing/Objects/Accessors/TestUmbracoContextAccessor.cs index da93218907..4f3b801af9 100644 --- a/src/Umbraco.Tests/Testing/Objects/Accessors/TestUmbracoContextAccessor.cs +++ b/src/Umbraco.Tests/Testing/Objects/Accessors/TestUmbracoContextAccessor.cs @@ -5,5 +5,14 @@ namespace Umbraco.Tests.Testing.Objects.Accessors public class TestUmbracoContextAccessor : IUmbracoContextAccessor { public UmbracoContext UmbracoContext { get; set; } + + public TestUmbracoContextAccessor() + { + } + + public TestUmbracoContextAccessor(UmbracoContext umbracoContext) + { + UmbracoContext = umbracoContext; + } } } diff --git a/src/Umbraco.Tests/Testing/TestingTests/MockTests.cs b/src/Umbraco.Tests/Testing/TestingTests/MockTests.cs index f21a953ae7..6b815bc434 100644 --- a/src/Umbraco.Tests/Testing/TestingTests/MockTests.cs +++ b/src/Umbraco.Tests/Testing/TestingTests/MockTests.cs @@ -7,6 +7,7 @@ using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Dictionary; +using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Services; @@ -65,7 +66,7 @@ namespace Umbraco.Tests.Testing.TestingTests Mock.Of(), Mock.Of(), Mock.Of(), - new MembershipHelper(umbracoContext, Mock.Of(), Mock.Of()), + new MembershipHelper(new TestUmbracoContextAccessor(umbracoContext), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), null), new ServiceContext(), CacheHelper.CreateDisabledCacheHelper()); Assert.Pass(); diff --git a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs index f9cff5af9b..dfa917459f 100644 --- a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs @@ -10,6 +10,7 @@ using Umbraco.Core.Cache; using Umbraco.Core.Composing; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Dictionary; +using Umbraco.Core.Logging; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Services; using Umbraco.Tests.TestHelpers; @@ -129,7 +130,7 @@ namespace Umbraco.Tests.Web.Mvc Mock.Of(), Mock.Of(), Mock.Of(), - new MembershipHelper(umbracoContext, Mock.Of(), Mock.Of()), + new MembershipHelper(new TestUmbracoContextAccessor(umbracoContext), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), null), new ServiceContext(), CacheHelper.CreateDisabledCacheHelper()); diff --git a/src/Umbraco.Web/Composing/CompositionRoots/HelperCompositionRoot.cs b/src/Umbraco.Web/Composing/CompositionRoots/HelperCompositionRoot.cs new file mode 100644 index 0000000000..d708493016 --- /dev/null +++ b/src/Umbraco.Web/Composing/CompositionRoots/HelperCompositionRoot.cs @@ -0,0 +1,17 @@ +using System.Web.Security; +using LightInject; +using Umbraco.Web.Security; +using Umbraco.Web.Security.Providers; + +namespace Umbraco.Web.Composing.CompositionRoots +{ + public class HelperCompositionRoot : ICompositionRoot + { + public void Compose(IServiceRegistry container) + { + container.Register((factory) => Core.Security.MembershipProviderExtensions.GetMembersMembershipProvider()); + container.Register((factory) => Roles.Enabled ? Roles.Provider : new MembersRoleProvider(Core.Composing.Current.Services.MemberService)); + container.Register(); + } + } +} diff --git a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs index 6b3581aeab..e39113c953 100644 --- a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs +++ b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs @@ -97,7 +97,7 @@ namespace Umbraco.Web } if (cacheByMember) { - var helper = new MembershipHelper(Current.UmbracoContext); + var helper = Current.Container.GetInstance(); var currentMember = helper.GetCurrentMember(); cacheKey.AppendFormat("m{0}-", currentMember == null ? 0 : currentMember.Id); } diff --git a/src/Umbraco.Web/Models/LoginStatusModel.cs b/src/Umbraco.Web/Models/LoginStatusModel.cs index 78425969dd..dd2b517b99 100644 --- a/src/Umbraco.Web/Models/LoginStatusModel.cs +++ b/src/Umbraco.Web/Models/LoginStatusModel.cs @@ -25,7 +25,7 @@ namespace Umbraco.Web.Models { if (doLookup && Current.UmbracoContext != null) { - var helper = new MembershipHelper(Current.UmbracoContext); + var helper = Current.Container.GetInstance(); var model = helper.GetCurrentLoginStatus(); if (model != null) { diff --git a/src/Umbraco.Web/Models/ProfileModel.cs b/src/Umbraco.Web/Models/ProfileModel.cs index c999657b6a..9c5bff0f7e 100644 --- a/src/Umbraco.Web/Models/ProfileModel.cs +++ b/src/Umbraco.Web/Models/ProfileModel.cs @@ -27,7 +27,7 @@ namespace Umbraco.Web.Models MemberProperties = new List(); if (doLookup && Current.UmbracoContext != null) { - var helper = new MembershipHelper(Current.UmbracoContext); + var helper = Current.Container.GetInstance(); var model = helper.GetCurrentMemberProfileModel(); MemberProperties = model.MemberProperties; } diff --git a/src/Umbraco.Web/Models/RegisterModel.cs b/src/Umbraco.Web/Models/RegisterModel.cs index 2851db54a4..e5a04b06bb 100644 --- a/src/Umbraco.Web/Models/RegisterModel.cs +++ b/src/Umbraco.Web/Models/RegisterModel.cs @@ -31,7 +31,7 @@ namespace Umbraco.Web.Models CreatePersistentLoginCookie = true; if (doLookup && Current.UmbracoContext != null) { - var helper = new MembershipHelper(Current.UmbracoContext); + var helper = Current.Container.GetInstance(); var model = helper.CreateRegistrationModel(MemberTypeAlias); MemberProperties = model.MemberProperties; } diff --git a/src/Umbraco.Web/Routing/PublishedRouter.cs b/src/Umbraco.Web/Routing/PublishedRouter.cs index 8e7d786e93..c6c4ef94c8 100644 --- a/src/Umbraco.Web/Routing/PublishedRouter.cs +++ b/src/Umbraco.Web/Routing/PublishedRouter.cs @@ -7,6 +7,7 @@ using System.IO; using System.Web.Security; using umbraco; using Umbraco.Core; +using Umbraco.Core.Composing; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.IO; using Umbraco.Core.Logging; @@ -633,7 +634,7 @@ namespace Umbraco.Web.Routing { _logger.Debug(() => $"{tracePrefix}Page is protected, check for access"); - var membershipHelper = new MembershipHelper(request.UmbracoContext); + var membershipHelper = Current.Container.GetInstance(); if (membershipHelper.IsLoggedIn() == false) { diff --git a/src/Umbraco.Web/Runtime/WebRuntimeComponent.cs b/src/Umbraco.Web/Runtime/WebRuntimeComponent.cs index a4e5db0767..91715fe016 100644 --- a/src/Umbraco.Web/Runtime/WebRuntimeComponent.cs +++ b/src/Umbraco.Web/Runtime/WebRuntimeComponent.cs @@ -70,6 +70,8 @@ namespace Umbraco.Web.Runtime //it still needs to use the install controller so we can't do that composition.Container.RegisterFrom(); + composition.Container.RegisterFrom(); + // register accessors for cultures composition.Container.RegisterSingleton(); composition.Container.RegisterSingleton(); diff --git a/src/Umbraco.Web/Security/MembershipHelper.cs b/src/Umbraco.Web/Security/MembershipHelper.cs index 7a56679086..a3923e9353 100644 --- a/src/Umbraco.Web/Security/MembershipHelper.cs +++ b/src/Umbraco.Web/Security/MembershipHelper.cs @@ -5,7 +5,6 @@ using System.Linq; using System.Text; using System.Web; using System.Web.Security; -using LightInject; using Umbraco.Core; using Umbraco.Core.Logging; using Umbraco.Core.Models; @@ -34,69 +33,53 @@ namespace Umbraco.Web.Security private readonly IPublishedMemberCache _memberCache; private readonly UmbracoContext _umbracoContext; - [Inject] - private IMemberService MemberService { get; set; } + private IMemberService MemberService { get; } - [Inject] - private IMemberTypeService MemberTypeService { get; set; } + private IMemberTypeService MemberTypeService { get; } - [Inject] - private IUserService UserService { get; set; } + private IUserService UserService { get; } - [Inject] - private IPublicAccessService PublicAccessService { get; set; } + private IPublicAccessService PublicAccessService { get; } + public CacheHelper CacheHelper { get; } - [Inject] - private CacheHelper ApplicationCache { get; set; } + private CacheHelper ApplicationCache { get; } - [Inject] - private ILogger Logger { get; set; } + private ILogger Logger { get; } - [Inject] - private PublishedRouter Router { get; set; } + private PublishedRouter Router { get; } #region Constructors - // used here and there for IMember operations (not front-end stuff, no need for _memberCache) - - [Obsolete("Use the constructor specifying an UmbracoContext")] - [EditorBrowsable(EditorBrowsableState.Never)] - public MembershipHelper(HttpContextBase httpContext) + public MembershipHelper + ( + IUmbracoContextAccessor accessor, + MembershipProvider membershipProvider, + RoleProvider roleProvider, + IMemberService memberService, + IMemberTypeService memberTypeService, + IUserService userService, + IPublicAccessService publicAccessService, + CacheHelper cacheHelper, + ILogger logger, + PublishedRouter router + ) { - if (httpContext == null) throw new ArgumentNullException(nameof(httpContext)); - _httpContext = httpContext; - _membershipProvider = MPE.GetMembersMembershipProvider(); - _roleProvider = Roles.Enabled ? Roles.Provider : new MembersRoleProvider(MemberService); - - // _memberCache remains null - not supposed to use it - // alternatively we'd need to get if from the 'current' UmbracoContext? - - // helpers are *not* instanciated by the container so we have to - // get our dependencies injected manually, through properties. - ((IServiceContainer)Current.Container.ConcreteContainer).InjectProperties(this); - } - - // used everywhere - public MembershipHelper(UmbracoContext umbracoContext) - : this(umbracoContext, MPE.GetMembersMembershipProvider(), Roles.Enabled ? Roles.Provider : new MembersRoleProvider(Current.Services.MemberService)) - { } - - // used in tests and (this) - public MembershipHelper(UmbracoContext umbracoContext, MembershipProvider membershipProvider, RoleProvider roleProvider) - { - if (umbracoContext == null) throw new ArgumentNullException(nameof(umbracoContext)); + if (accessor.UmbracoContext == null) throw new ArgumentNullException(nameof(accessor)); if (membershipProvider == null) throw new ArgumentNullException(nameof(membershipProvider)); if (roleProvider == null) throw new ArgumentNullException(nameof(roleProvider)); + MemberService = memberService; + MemberTypeService = memberTypeService; + UserService = userService; + PublicAccessService = publicAccessService; + CacheHelper = cacheHelper; + Logger = logger; + Router = router; - _httpContext = umbracoContext.HttpContext; - _umbracoContext = umbracoContext; + _httpContext = accessor.UmbracoContext.HttpContext; + _umbracoContext = accessor.UmbracoContext; _membershipProvider = membershipProvider; _roleProvider = roleProvider; - _memberCache = umbracoContext.PublishedSnapshot.Members; - - // helpers are *not* instanciated by the container so we have to - // get our dependencies injected manually, through properties. - ((IServiceContainer)Current.Container.ConcreteContainer).InjectProperties(this); + _memberCache = accessor.UmbracoContext.PublishedSnapshot.Members; } #endregion diff --git a/src/Umbraco.Web/Security/WebSecurity.cs b/src/Umbraco.Web/Security/WebSecurity.cs index 5de163b638..1dd0f6f34c 100644 --- a/src/Umbraco.Web/Security/WebSecurity.cs +++ b/src/Umbraco.Web/Security/WebSecurity.cs @@ -57,7 +57,7 @@ namespace Umbraco.Web.Security { return false; } - var helper = new MembershipHelper(Current.UmbracoContext); + var helper = Current.Container.GetInstance(); return helper.IsMemberAuthorized(allowAll, allowTypes, allowGroups, allowMembers); } diff --git a/src/Umbraco.Web/UI/Controls/UmbracoUserControl.cs b/src/Umbraco.Web/UI/Controls/UmbracoUserControl.cs index 2875cb82bd..919a614034 100644 --- a/src/Umbraco.Web/UI/Controls/UmbracoUserControl.cs +++ b/src/Umbraco.Web/UI/Controls/UmbracoUserControl.cs @@ -33,7 +33,7 @@ namespace Umbraco.Web.UI.Controls if (umbracoContext == null) throw new ArgumentNullException(nameof(umbracoContext)); UmbracoContext = umbracoContext; Umbraco = new UmbracoHelper(umbracoContext, services, appCache); - Members = new MembershipHelper(umbracoContext); + Members = Current.Container.GetInstance(); // fixme inject somehow Logger = Current.Logger; diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index f9708e5c04..2e06668954 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -109,6 +109,7 @@ + diff --git a/src/Umbraco.Web/UmbracoHelper.cs b/src/Umbraco.Web/UmbracoHelper.cs index 7375dac98d..a20a08fd76 100644 --- a/src/Umbraco.Web/UmbracoHelper.cs +++ b/src/Umbraco.Web/UmbracoHelper.cs @@ -160,7 +160,7 @@ namespace Umbraco.Web /// Gets the membership helper. /// public MembershipHelper MembershipHelper => _membershipHelper - ?? (_membershipHelper = new MembershipHelper(UmbracoContext)); + ?? (_membershipHelper = Current.Container.GetInstance()); /// /// Gets the url provider.