Moved creation of container into ContainerFactory
This commit is contained in:
37
src/Umbraco.Core/Composing/ContainerFactory.cs
Normal file
37
src/Umbraco.Core/Composing/ContainerFactory.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Reflection;
|
||||
using Umbraco.Core.Composing.LightInject;
|
||||
|
||||
namespace Umbraco.Core.Composing
|
||||
{
|
||||
public class ContainerFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -160,6 +160,7 @@
|
||||
<Compile Include="Composing\Composers\FileSystemsComposer.cs" />
|
||||
<Compile Include="Composing\Composers\RepositoriesComposer.cs" />
|
||||
<Compile Include="Composing\Composers\ServicesComposer.cs" />
|
||||
<Compile Include="Composing\ContainerFactory.cs" />
|
||||
<Compile Include="Composing\Current.cs" />
|
||||
<Compile Include="Composing\HideFromTypeFinderAttribute.cs" />
|
||||
<Compile Include="Composing\IBuilderCollection.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
|
||||
|
||||
@@ -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<IServerRegistrar>(_ => new TestServerRegistrar());
|
||||
container.RegisterSingleton<IServerMessenger>(_ => new TestServerMessenger());
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Umbraco.Tests.Composing
|
||||
{
|
||||
Current.Reset();
|
||||
|
||||
_container = Current.Container = Core.Composing.LightInject.LightInjectContainer.Create();
|
||||
_container = Current.Container = ContainerFactory.Create();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<PackageActionCollectionBuilder>()
|
||||
.Add(() => TypeLoader.GetPackageActions());
|
||||
|
||||
@@ -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<ILogger>());
|
||||
_container.Register<FileSystems>();
|
||||
|
||||
@@ -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<PropertyValueConverterCollectionBuilder>();
|
||||
|
||||
|
||||
@@ -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<IShortStringHelper>(_
|
||||
=> new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(SettingsForTests.GetDefaultUmbracoSettings())));
|
||||
|
||||
@@ -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<PropertyValueConverterCollectionBuilder>()
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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<ILogger>(factory => Mock.Of<ILogger>());
|
||||
container.RegisterSingleton<IProfiler>(factory => Mock.Of<IProfiler>());
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user