From 48437adb4701deef12a5580a06607f6c6362c612 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 7 Nov 2019 08:00:48 +0100 Subject: [PATCH] Move Configs to abstractions --- .../Configuration/Configs.cs | 20 ++++++++++++------- .../Configuration}/IConfigsFactory.cs | 4 +--- src/Umbraco.Abstractions/Services/IRuntime.cs | 3 ++- .../ConfigsFactory.cs | 3 +-- src/Umbraco.Core/Configuration/CoreDebug.cs | 3 ++- src/Umbraco.Core/Runtime/CoreRuntime.cs | 12 +++++------ src/Umbraco.Core/Umbraco.Core.csproj | 3 +-- .../DistributedCache/DistributedCacheTests.cs | 1 + .../Composing/CollectionBuildersTests.cs | 1 + .../Composing/CompositionTests.cs | 1 + .../Composing/PackageActionCollectionTests.cs | 1 + src/Umbraco.Tests/IO/FileSystemsTests.cs | 1 + src/Umbraco.Tests/Macros/MacroTests.cs | 1 + src/Umbraco.Tests/Models/MacroTests.cs | 1 + src/Umbraco.Tests/Models/MemberTests.cs | 1 + src/Umbraco.Tests/Models/UserTests.cs | 1 + .../PropertyEditorValueEditorTests.cs | 1 + .../Published/ConvertersTests.cs | 1 + .../Runtimes/CoreRuntimeTests.cs | 8 ++++---- .../Scoping/ScopeEventDispatcherTests.cs | 1 + src/Umbraco.Web/Runtime/WebRuntime.cs | 4 ++-- src/Umbraco.Web/UmbracoApplicationBase.cs | 3 ++- 22 files changed, 46 insertions(+), 29 deletions(-) rename src/{Umbraco.Core => Umbraco.Abstractions}/Configuration/Configs.cs (83%) rename src/{Umbraco.Core/Composing => Umbraco.Abstractions/Configuration}/IConfigsFactory.cs (53%) rename src/Umbraco.Core/{Composing => Configuration}/ConfigsFactory.cs (81%) diff --git a/src/Umbraco.Core/Configuration/Configs.cs b/src/Umbraco.Abstractions/Configuration/Configs.cs similarity index 83% rename from src/Umbraco.Core/Configuration/Configs.cs rename to src/Umbraco.Abstractions/Configuration/Configs.cs index 51e1a327a0..e08c4097e4 100644 --- a/src/Umbraco.Core/Configuration/Configs.cs +++ b/src/Umbraco.Abstractions/Configuration/Configs.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Configuration; using Umbraco.Core.Composing; -using Umbraco.Core.Logging; namespace Umbraco.Core.Configuration { @@ -15,6 +13,13 @@ namespace Umbraco.Core.Configuration /// public class Configs { + private readonly Func _configGetter; + + public Configs(Func configGetter) + { + _configGetter = configGetter; + } + private readonly Dictionary> _configs = new Dictionary>(); private Dictionary> _registerings = new Dictionary>(); @@ -60,7 +65,7 @@ namespace Umbraco.Core.Configuration _configs[typeOfConfig] = new Lazy(() => { - if (Current.HasFactory) return Current.Factory.GetInstance(); + if (CurrentCore.HasFactory) return CurrentCore.Factory.GetInstance(); throw new InvalidOperationException($"Cannot get configuration of type {typeOfConfig} during composition."); }); _registerings[typeOfConfig] = register => register.Register(configFactory, Lifetime.Singleton); @@ -75,7 +80,7 @@ namespace Umbraco.Core.Configuration Add(() => GetConfig(sectionName)); } - private static TConfig GetConfig(string sectionName) + private TConfig GetConfig(string sectionName) where TConfig : class { // note: need to use SafeCallContext here because ConfigurationManager.GetSection ends up getting AppDomain.Evidence @@ -83,10 +88,11 @@ namespace Umbraco.Core.Configuration using (new SafeCallContext()) { - if ((ConfigurationManager.GetSection(sectionName) is TConfig config)) + if ((_configGetter(sectionName) is TConfig config)) return config; - var ex = new ConfigurationErrorsException($"Could not get configuration section \"{sectionName}\" from config files."); - Current.Logger.Error(ex, "Config error"); +// var ex = new ConfigurationErrorsException($"Could not get configuration section \"{sectionName}\" from config files."); + var ex = new Exception($"Could not get configuration section \"{sectionName}\" from config files."); +// Current.Logger.Error(ex, "Config error"); throw ex; } } diff --git a/src/Umbraco.Core/Composing/IConfigsFactory.cs b/src/Umbraco.Abstractions/Configuration/IConfigsFactory.cs similarity index 53% rename from src/Umbraco.Core/Composing/IConfigsFactory.cs rename to src/Umbraco.Abstractions/Configuration/IConfigsFactory.cs index 1b12f5e157..3e36f3fb55 100644 --- a/src/Umbraco.Core/Composing/IConfigsFactory.cs +++ b/src/Umbraco.Abstractions/Configuration/IConfigsFactory.cs @@ -1,6 +1,4 @@ -using Umbraco.Core.Configuration; - -namespace Umbraco.Core.Composing +namespace Umbraco.Core.Configuration { public interface IConfigsFactory { diff --git a/src/Umbraco.Abstractions/Services/IRuntime.cs b/src/Umbraco.Abstractions/Services/IRuntime.cs index d433dde12d..a7944ad256 100644 --- a/src/Umbraco.Abstractions/Services/IRuntime.cs +++ b/src/Umbraco.Abstractions/Services/IRuntime.cs @@ -1,4 +1,5 @@ using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; namespace Umbraco.Core { @@ -12,7 +13,7 @@ namespace Umbraco.Core /// /// The application register. /// The application factory. - IFactory Boot(IRegister register); + IFactory Boot(IRegister register, IConfigsFactory configsFactory); /// /// Gets the runtime state. diff --git a/src/Umbraco.Core/Composing/ConfigsFactory.cs b/src/Umbraco.Core/Configuration/ConfigsFactory.cs similarity index 81% rename from src/Umbraco.Core/Composing/ConfigsFactory.cs rename to src/Umbraco.Core/Configuration/ConfigsFactory.cs index 995ab4e3aa..aeeef1e653 100644 --- a/src/Umbraco.Core/Composing/ConfigsFactory.cs +++ b/src/Umbraco.Core/Configuration/ConfigsFactory.cs @@ -1,7 +1,6 @@ using System.Configuration; -using Umbraco.Core.Configuration; -namespace Umbraco.Core.Composing +namespace Umbraco.Core.Configuration { public class ConfigsFactory : IConfigsFactory { diff --git a/src/Umbraco.Core/Configuration/CoreDebug.cs b/src/Umbraco.Core/Configuration/CoreDebug.cs index b24e8a3329..aa95a25990 100644 --- a/src/Umbraco.Core/Configuration/CoreDebug.cs +++ b/src/Umbraco.Core/Configuration/CoreDebug.cs @@ -1,4 +1,5 @@ using System; +using System.Configuration; namespace Umbraco.Core.Configuration { @@ -6,7 +7,7 @@ namespace Umbraco.Core.Configuration { public CoreDebug() { - var appSettings = System.Configuration.ConfigurationManager.AppSettings; + var appSettings = ConfigurationManager.AppSettings; LogUncompletedScopes = string.Equals("true", appSettings[Constants.AppSettings.Debug.LogUncompletedScopes], StringComparison.OrdinalIgnoreCase); DumpOnTimeoutThreadAbort = string.Equals("true", appSettings[Constants.AppSettings.Debug.DumpOnTimeoutThreadAbort], StringComparison.OrdinalIgnoreCase); } diff --git a/src/Umbraco.Core/Runtime/CoreRuntime.cs b/src/Umbraco.Core/Runtime/CoreRuntime.cs index d9c8ff05d3..237dcece64 100644 --- a/src/Umbraco.Core/Runtime/CoreRuntime.cs +++ b/src/Umbraco.Core/Runtime/CoreRuntime.cs @@ -47,7 +47,7 @@ namespace Umbraco.Core.Runtime public IRuntimeState State => _state; /// - public virtual IFactory Boot(IRegister register) + public virtual IFactory Boot(IRegister register, IConfigsFactory configsFactory) { // create and register the essential services // ie the bare minimum required to boot @@ -82,7 +82,7 @@ namespace Umbraco.Core.Runtime ConfigureUnhandledException(); ConfigureApplicationRootPath(); - Boot(register, timer); + Boot(register, timer, configsFactory); } return _factory; @@ -91,7 +91,7 @@ namespace Umbraco.Core.Runtime /// /// Boots the runtime within a timer. /// - protected virtual IFactory Boot(IRegister register, DisposableTimer timer) + protected virtual IFactory Boot(IRegister register, DisposableTimer timer, IConfigsFactory configsFactory) { Composition composition = null; @@ -110,7 +110,7 @@ namespace Umbraco.Core.Runtime var databaseFactory = GetDatabaseFactory(); // configs - var configs = GetConfigs(); + var configs = GetConfigs(configsFactory); // type loader var typeLoader = new TypeLoader(appCaches.RuntimeCache, configs.Global().LocalTempPath, ProfilingLogger); @@ -344,9 +344,9 @@ namespace Umbraco.Core.Runtime /// /// Gets the configurations. /// - protected virtual Configs GetConfigs() + protected virtual Configs GetConfigs(IConfigsFactory configsFactory) { - var configs = new ConfigsFactory().Create(); + var configs = configsFactory.Create(); return configs; } diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index f40f81ef71..5755532cd6 100755 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -158,9 +158,7 @@ - - @@ -176,6 +174,7 @@ + diff --git a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs index 54318ace15..b752cb6a90 100644 --- a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs +++ b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs @@ -6,6 +6,7 @@ using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Sync; using Umbraco.Tests.Components; diff --git a/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs b/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs index 4303f59916..25d46a3cdc 100644 --- a/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs +++ b/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs @@ -5,6 +5,7 @@ using Moq; using NUnit.Framework; using Umbraco.Core.Composing; using Umbraco.Core; +using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Tests.Components; diff --git a/src/Umbraco.Tests/Composing/CompositionTests.cs b/src/Umbraco.Tests/Composing/CompositionTests.cs index 72f7e99ca6..06c9c0b786 100644 --- a/src/Umbraco.Tests/Composing/CompositionTests.cs +++ b/src/Umbraco.Tests/Composing/CompositionTests.cs @@ -4,6 +4,7 @@ using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.IO; using Umbraco.Core.Logging; diff --git a/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs b/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs index 57f691eea5..0073a4d624 100644 --- a/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs +++ b/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs @@ -6,6 +6,7 @@ using Moq; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.PackageActions; using Umbraco.Tests.Components; diff --git a/src/Umbraco.Tests/IO/FileSystemsTests.cs b/src/Umbraco.Tests/IO/FileSystemsTests.cs index 28c1817862..2a32a1bc27 100644 --- a/src/Umbraco.Tests/IO/FileSystemsTests.cs +++ b/src/Umbraco.Tests/IO/FileSystemsTests.cs @@ -13,6 +13,7 @@ using Umbraco.Core.Services; using Umbraco.Tests.Components; using Umbraco.Tests.TestHelpers; using Umbraco.Core.Composing.CompositionExtensions; +using Umbraco.Core.Configuration; using FileSystems = Umbraco.Core.IO.FileSystems; namespace Umbraco.Tests.IO diff --git a/src/Umbraco.Tests/Macros/MacroTests.cs b/src/Umbraco.Tests/Macros/MacroTests.cs index 4700edc64b..e813424ad6 100644 --- a/src/Umbraco.Tests/Macros/MacroTests.cs +++ b/src/Umbraco.Tests/Macros/MacroTests.cs @@ -2,6 +2,7 @@ using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers; using Umbraco.Web.Macros; diff --git a/src/Umbraco.Tests/Models/MacroTests.cs b/src/Umbraco.Tests/Models/MacroTests.cs index 117301815e..e96a8c7915 100644 --- a/src/Umbraco.Tests/Models/MacroTests.cs +++ b/src/Umbraco.Tests/Models/MacroTests.cs @@ -2,6 +2,7 @@ using System.Linq; using NUnit.Framework; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.Models; using Umbraco.Core.Models.Entities; using Umbraco.Tests.TestHelpers; diff --git a/src/Umbraco.Tests/Models/MemberTests.cs b/src/Umbraco.Tests/Models/MemberTests.cs index 9b478304d1..666b9a8ab1 100644 --- a/src/Umbraco.Tests/Models/MemberTests.cs +++ b/src/Umbraco.Tests/Models/MemberTests.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Linq; using NUnit.Framework; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.Models; using Umbraco.Core.Serialization; using Umbraco.Tests.TestHelpers; diff --git a/src/Umbraco.Tests/Models/UserTests.cs b/src/Umbraco.Tests/Models/UserTests.cs index 8cc66d5eb0..b8151fc944 100644 --- a/src/Umbraco.Tests/Models/UserTests.cs +++ b/src/Umbraco.Tests/Models/UserTests.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Linq; using NUnit.Framework; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.Models.Membership; using Umbraco.Core.Serialization; using Umbraco.Tests.TestHelpers; diff --git a/src/Umbraco.Tests/PropertyEditors/PropertyEditorValueEditorTests.cs b/src/Umbraco.Tests/PropertyEditors/PropertyEditorValueEditorTests.cs index 348305fbf5..c1b80b97b1 100644 --- a/src/Umbraco.Tests/PropertyEditors/PropertyEditorValueEditorTests.cs +++ b/src/Umbraco.Tests/PropertyEditors/PropertyEditorValueEditorTests.cs @@ -4,6 +4,7 @@ using Moq; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.PropertyEditors; diff --git a/src/Umbraco.Tests/Published/ConvertersTests.cs b/src/Umbraco.Tests/Published/ConvertersTests.cs index f3dd459ba3..01e14edf12 100644 --- a/src/Umbraco.Tests/Published/ConvertersTests.cs +++ b/src/Umbraco.Tests/Published/ConvertersTests.cs @@ -5,6 +5,7 @@ using Moq; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; diff --git a/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs b/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs index 449cddfcc6..daf4f4afe6 100644 --- a/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs +++ b/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs @@ -102,9 +102,9 @@ namespace Umbraco.Tests.Runtimes return mock.Object; } - protected override Configs GetConfigs() + protected override Configs GetConfigs(IConfigsFactory configsFactory) { - var configs = new ConfigsFactory().Create(); + var configs = configsFactory.Create(); configs.Add(SettingsForTests.GetDefaultGlobalSettings); configs.Add(SettingsForTests.GetDefaultUmbracoSettings); return configs; @@ -142,9 +142,9 @@ namespace Umbraco.Tests.Runtimes private IMainDom _mainDom; - public override IFactory Boot(IRegister container) + public override IFactory Boot(IRegister container, IConfigsFactory configsFactory) { - var factory = base.Boot(container); + var factory = base.Boot(container, configsFactory); _mainDom = factory.GetInstance(); return factory; } diff --git a/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs b/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs index b964f89be8..1597ef35cc 100644 --- a/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs @@ -11,6 +11,7 @@ using Umbraco.Core.Scoping; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.Persistence.Mappers; using Umbraco.Core.Services; using Umbraco.Tests.Components; diff --git a/src/Umbraco.Web/Runtime/WebRuntime.cs b/src/Umbraco.Web/Runtime/WebRuntime.cs index 13cd717fd1..ede500e599 100644 --- a/src/Umbraco.Web/Runtime/WebRuntime.cs +++ b/src/Umbraco.Web/Runtime/WebRuntime.cs @@ -27,7 +27,7 @@ namespace Umbraco.Web.Runtime } /// - public override IFactory Boot(IRegister register) + public override IFactory Boot(IRegister register, IConfigsFactory configsFactory) { // create and start asap to profile boot var debug = GlobalSettings.DebugMode; @@ -43,7 +43,7 @@ namespace Umbraco.Web.Runtime _webProfiler = new VoidProfiler(); } - var factory = base.Boot(register); + var factory = base.Boot(register, configsFactory); // now (and only now) is the time to switch over to perWebRequest scopes. // up until that point we may not have a request, and scoped services would diff --git a/src/Umbraco.Web/UmbracoApplicationBase.cs b/src/Umbraco.Web/UmbracoApplicationBase.cs index 59b5b1779b..24800500a8 100644 --- a/src/Umbraco.Web/UmbracoApplicationBase.cs +++ b/src/Umbraco.Web/UmbracoApplicationBase.cs @@ -5,6 +5,7 @@ using System.Web; using System.Web.Hosting; using Umbraco.Core; using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Logging.Serilog; @@ -62,7 +63,7 @@ namespace Umbraco.Web // the boot manager is responsible for registrations var register = GetRegister(); _runtime = GetRuntime(); - _runtime.Boot(register); + _runtime.Boot(register, new ConfigsFactory()); } // called by ASP.NET (auto event wireup) once per app domain