From caa2802c16acaf214db00ec13bddc745ef149c26 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Fri, 27 Jul 2018 21:16:11 +0200 Subject: [PATCH] Moved creation of container into ContainerFactory --- .../Composing/ContainerFactory.cs | 37 +++++++++++++++++++ src/Umbraco.Core/Umbraco.Core.csproj | 1 + src/Umbraco.Core/UmbracoApplicationBase.cs | 2 +- .../DistributedCache/DistributedCacheTests.cs | 2 +- .../Composing/CollectionBuildersTests.cs | 2 +- .../Composing/ContainerImplementationTests.cs | 2 +- .../Composing/LazyCollectionBuilderTests.cs | 2 +- .../Composing/PackageActionCollectionTests.cs | 2 +- src/Umbraco.Tests/IO/FileSystemsTests.cs | 2 +- .../PropertyEditors/ImageCropperTest.cs | 2 +- .../PropertyEditorValueEditorTests.cs | 2 +- .../Published/ConvertersTests.cs | 2 +- .../Scoping/ScopeEventDispatcherTests.cs | 2 +- .../TestHelpers/BaseUsingSqlCeSyntax.cs | 2 +- src/Umbraco.Tests/Testing/UmbracoTestBase.cs | 2 +- 15 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 src/Umbraco.Core/Composing/ContainerFactory.cs diff --git a/src/Umbraco.Core/Composing/ContainerFactory.cs b/src/Umbraco.Core/Composing/ContainerFactory.cs new file mode 100644 index 0000000000..70e04eb2ed --- /dev/null +++ b/src/Umbraco.Core/Composing/ContainerFactory.cs @@ -0,0 +1,37 @@ +using System; +using System.Configuration; +using System.Reflection; +using Umbraco.Core.Composing.LightInject; + +namespace Umbraco.Core.Composing +{ + public class ContainerFactory + { + /// + /// Creates a new instance of the configured container. + /// To override the default LightInjectContainer, add an appSetting named umbracoContainerType with + /// a fully qualified type name to a class with a static method "Create" returning an IContainer. + /// + public static IContainer Create() + { + var configuredTypeName = ConfigurationManager.AppSettings["umbracoContainerType"] + ?? typeof(LightInjectContainer).AssemblyQualifiedName; + var type = Type.GetType(configuredTypeName); + if (type == null) + { + throw new Exception($"Cannot find container factory class named '${configuredTypeName}'"); + } + var factoryMethod = type.GetMethod("Create", BindingFlags.Static); + if (factoryMethod == null) + { + throw new Exception($"Container factory class '${configuredTypeName}' does not have a public static method named Create"); + } + var container = factoryMethod.Invoke(null, new object[0]) as IContainer; + if (container == null) + { + throw new Exception($"Container factory '${configuredTypeName}' did not return an IContainer implementation."); + } + return container; + } + } +} diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 412b55ea71..b9b682de33 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -160,6 +160,7 @@ + diff --git a/src/Umbraco.Core/UmbracoApplicationBase.cs b/src/Umbraco.Core/UmbracoApplicationBase.cs index 5d40a64002..2e08814e1f 100644 --- a/src/Umbraco.Core/UmbracoApplicationBase.cs +++ b/src/Umbraco.Core/UmbracoApplicationBase.cs @@ -36,7 +36,7 @@ namespace Umbraco.Core { // note: the actual, web UmbracoApplication is overriding this // with a web-supporting container - return Composing.LightInject.LightInjectContainer.Create(); + return ContainerFactory.Create(); } // events - in the order they trigger diff --git a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs index 07cb2b8319..554194fd91 100644 --- a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs +++ b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs @@ -20,7 +20,7 @@ namespace Umbraco.Tests.Cache.DistributedCache [SetUp] public void Setup() { - var container = Current.Container = Core.Composing.LightInject.LightInjectContainer.Create(); + var container = Current.Container = ContainerFactory.Create(); container.Register(_ => new TestServerRegistrar()); container.RegisterSingleton(_ => new TestServerMessenger()); diff --git a/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs b/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs index 4e254ce425..1bddbdfbb2 100644 --- a/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs +++ b/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs @@ -17,7 +17,7 @@ namespace Umbraco.Tests.Composing { Current.Reset(); - _container = Current.Container = Core.Composing.LightInject.LightInjectContainer.Create(); + _container = Current.Container = ContainerFactory.Create(); } [TearDown] diff --git a/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs b/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs index 46a2d08256..818bf47cd0 100644 --- a/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs +++ b/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs @@ -10,7 +10,7 @@ namespace Umbraco.Tests.Composing [TestFixture] public class ContainerImplementationTests { - private IContainer CreateContainer() => Core.Composing.LightInject.LightInjectContainer.Create(); + private IContainer CreateContainer() => ContainerFactory.Create(); [Test] public void CanRegisterSingletonInterface() diff --git a/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs b/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs index d28820f59f..e0dde277f9 100644 --- a/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs +++ b/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs @@ -23,7 +23,7 @@ namespace Umbraco.Tests.Composing } private IContainer CreateContainer() - => Current.Container = Core.Composing.LightInject.LightInjectContainer.Create(); + => Current.Container = ContainerFactory.Create(); // note // lazy collection builder does not throw on duplicate, just uses distinct types diff --git a/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs b/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs index c49dd4b83f..236d92b94e 100644 --- a/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs +++ b/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs @@ -13,7 +13,7 @@ namespace Umbraco.Tests.Composing [Test] public void PackageActionCollectionBuilderWorks() { - var container = Current.Container = Core.Composing.LightInject.LightInjectContainer.Create(); + var container = Current.Container = ContainerFactory.Create(); container.RegisterCollectionBuilder() .Add(() => TypeLoader.GetPackageActions()); diff --git a/src/Umbraco.Tests/IO/FileSystemsTests.cs b/src/Umbraco.Tests/IO/FileSystemsTests.cs index e265793239..f97bdc4189 100644 --- a/src/Umbraco.Tests/IO/FileSystemsTests.cs +++ b/src/Umbraco.Tests/IO/FileSystemsTests.cs @@ -26,7 +26,7 @@ namespace Umbraco.Tests.IO var config = SettingsForTests.GetDefaultUmbracoSettings(); SettingsForTests.ConfigureSettings(config); - _container = Current.Container = Core.Composing.LightInject.LightInjectContainer.Create(); + _container = Current.Container = ContainerFactory.Create(); _container.Register(_ => Mock.Of()); _container.Register(); diff --git a/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs b/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs index cb739edd92..2c5bc621eb 100644 --- a/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs +++ b/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs @@ -65,7 +65,7 @@ namespace Umbraco.Tests.PropertyEditors { try { - var container = Current.Container = Core.Composing.LightInject.LightInjectContainer.Create(); + var container = Current.Container = ContainerFactory.Create(); container.RegisterCollectionBuilder(); diff --git a/src/Umbraco.Tests/PropertyEditors/PropertyEditorValueEditorTests.cs b/src/Umbraco.Tests/PropertyEditors/PropertyEditorValueEditorTests.cs index 7efb8734c4..00ca646360 100644 --- a/src/Umbraco.Tests/PropertyEditors/PropertyEditorValueEditorTests.cs +++ b/src/Umbraco.Tests/PropertyEditors/PropertyEditorValueEditorTests.cs @@ -21,7 +21,7 @@ namespace Umbraco.Tests.PropertyEditors //normalize culture Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; - var container = Current.Container = Core.Composing.LightInject.LightInjectContainer.Create(); + var container = Current.Container = ContainerFactory.Create(); container.Register(_ => new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(SettingsForTests.GetDefaultUmbracoSettings()))); diff --git a/src/Umbraco.Tests/Published/ConvertersTests.cs b/src/Umbraco.Tests/Published/ConvertersTests.cs index 5447a8b368..c0ae68d7d5 100644 --- a/src/Umbraco.Tests/Published/ConvertersTests.cs +++ b/src/Umbraco.Tests/Published/ConvertersTests.cs @@ -171,7 +171,7 @@ namespace Umbraco.Tests.Published public void SimpleConverter3Test() { Current.Reset(); - var container = Current.Container = Core.Composing.LightInject.LightInjectContainer.Create(); + var container = Current.Container = ContainerFactory.Create(); Current.Container.RegisterCollectionBuilder() diff --git a/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs b/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs index 89d361e789..9d08229563 100644 --- a/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs @@ -29,7 +29,7 @@ namespace Umbraco.Tests.Scoping DoThing2 = null; DoThing3 = null; - var container = Current.Container = Core.Composing.LightInject.LightInjectContainer.Create(); + var container = Current.Container = ContainerFactory.Create(); _testObjects = new TestObjects(container); diff --git a/src/Umbraco.Tests/TestHelpers/BaseUsingSqlCeSyntax.cs b/src/Umbraco.Tests/TestHelpers/BaseUsingSqlCeSyntax.cs index 9ae9fd4700..4314af38b5 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseUsingSqlCeSyntax.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseUsingSqlCeSyntax.cs @@ -32,7 +32,7 @@ namespace Umbraco.Tests.TestHelpers var sqlSyntax = new SqlCeSyntaxProvider(); - var container = Current.Container = Core.Composing.LightInject.LightInjectContainer.Create(); + var container = Current.Container = ContainerFactory.Create(); container.RegisterSingleton(factory => Mock.Of()); container.RegisterSingleton(factory => Mock.Of()); diff --git a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs index 60c55f6406..7d9dd34491 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs @@ -113,7 +113,7 @@ namespace Umbraco.Tests.Testing // but hey, never know, better avoid garbage-in Reset(); - Container = Current.Container = Core.Composing.LightInject.LightInjectContainer.Create(); + Container = Current.Container = ContainerFactory.Create(); TestObjects = new TestObjects(Container);