Cleanup DI
This commit is contained in:
@@ -1,36 +1,54 @@
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Reflection;
|
||||
using Umbraco.Core.Composing.LightInject;
|
||||
|
||||
namespace Umbraco.Core.Composing
|
||||
{
|
||||
public class ContainerFactory
|
||||
/// <summary>
|
||||
/// Creates the container.
|
||||
/// </summary>
|
||||
public static class ContainerFactory
|
||||
{
|
||||
// cannot use typeof().AssemblyQualifiedName on the web container - we don't reference it
|
||||
// a normal Umbraco site should run on the web container, but an app may run on the core one
|
||||
private const string CoreLightInjectContainerTypeName = "Umbraco.Core.Composing.LightInject.LightInjectContainer,Umbraco.Core";
|
||||
private const string WebLightInjectContainerTypeName = "Umbraco.Web.Composing.LightInject.LightInjectContainer,Umbraco.Web";
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the configured container.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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>
|
||||
/// </remarks>
|
||||
public static IContainer Create()
|
||||
{
|
||||
var configuredTypeName = ConfigurationManager.AppSettings["umbracoContainerType"]
|
||||
?? typeof(LightInjectContainer).AssemblyQualifiedName;
|
||||
var type = Type.GetType(configuredTypeName);
|
||||
Type type;
|
||||
|
||||
var configuredTypeName = ConfigurationManager.AppSettings["umbracoContainerType"];
|
||||
if (configuredTypeName.IsNullOrWhiteSpace())
|
||||
{
|
||||
// try to get the web LightInject container type,
|
||||
// else the core LightInject container type
|
||||
type = Type.GetType(configuredTypeName = WebLightInjectContainerTypeName) ??
|
||||
Type.GetType(configuredTypeName = CoreLightInjectContainerTypeName);
|
||||
}
|
||||
else
|
||||
{
|
||||
// try to get the configured container type
|
||||
type = Type.GetType(configuredTypeName);
|
||||
}
|
||||
|
||||
if (type == null)
|
||||
{
|
||||
throw new Exception($"Cannot find container factory class named '${configuredTypeName}'");
|
||||
}
|
||||
var factoryMethod = type.GetMethod("Create", BindingFlags.Static);
|
||||
throw new Exception($"Cannot find container factory class '{configuredTypeName}'.");
|
||||
|
||||
var factoryMethod = type.GetMethod("Create", BindingFlags.Public | 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;
|
||||
throw new Exception($"Container factory class '{configuredTypeName}' does not have a public static method named Create.");
|
||||
|
||||
var container = factoryMethod.Invoke(null, Array.Empty<object>()) as IContainer;
|
||||
if (container == null)
|
||||
{
|
||||
throw new Exception($"Container factory '${configuredTypeName}' did not return an IContainer implementation.");
|
||||
}
|
||||
throw new Exception($"Container factory '{configuredTypeName}' did not return an IContainer implementation.");
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ namespace Umbraco.Core.Runtime
|
||||
composition.Container.RegisterCollectionBuilder<PostMigrationCollectionBuilder>()
|
||||
.Add(factory => factory.GetInstance<TypeLoader>().GetTypes<IPostMigration>());
|
||||
|
||||
composition.Container.RegisterSingleton<IMigrationBuilder, MigrationBuilder>();
|
||||
composition.Container.RegisterSingleton<IMigrationBuilder>(factory => new MigrationBuilder(composition.Container));
|
||||
|
||||
// by default, register a noop factory
|
||||
composition.Container.RegisterSingleton<IPublishedModelFactory, NoopPublishedModelFactory>();
|
||||
|
||||
@@ -70,7 +70,6 @@ namespace Umbraco.Core
|
||||
// create the container for the application, and configure.
|
||||
// the boot manager is responsible for registrations
|
||||
var container = GetContainer();
|
||||
container.RegisterSingleton(x => container);
|
||||
Current.Container = container;
|
||||
|
||||
// register the essential stuff,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Composing;
|
||||
using Umbraco.Web.Runtime;
|
||||
|
||||
namespace Umbraco.Web
|
||||
@@ -13,10 +12,5 @@ namespace Umbraco.Web
|
||||
{
|
||||
return new WebRuntime(this);
|
||||
}
|
||||
|
||||
protected override IContainer GetContainer()
|
||||
{
|
||||
return Composing.LightInject.LightInjectContainer.Create();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user