diff --git a/src/Umbraco.Core/Components/Composition.cs b/src/Umbraco.Core/Components/Composition.cs
index 6032dd303d..c81fe388ba 100644
--- a/src/Umbraco.Core/Components/Composition.cs
+++ b/src/Umbraco.Core/Components/Composition.cs
@@ -78,13 +78,17 @@ namespace Umbraco.Core.Components
=> _register.RegisterAuto(serviceBaseType);
///
- public IContainer ConfigureForWeb()
+ public void ConfigureForWeb()
=> _register.ConfigureForWeb();
///
- public IContainer EnablePerWebRequestScope()
+ public void EnablePerWebRequestScope()
=> _register.EnablePerWebRequestScope();
+ ///
+ public IFactory CreateFactory()
+ => _register.CreateFactory();
+
#endregion
#region Collection Builders
diff --git a/src/Umbraco.Core/Composing/FactoryExtensions.cs b/src/Umbraco.Core/Composing/FactoryExtensions.cs
new file mode 100644
index 0000000000..2640b7f7e6
--- /dev/null
+++ b/src/Umbraco.Core/Composing/FactoryExtensions.cs
@@ -0,0 +1,87 @@
+using System;
+using System.Linq;
+using System.Reflection;
+
+namespace Umbraco.Core.Composing
+{
+ ///
+ /// Provides extension methods to the class.
+ ///
+ public static class FactoryExtensions
+ {
+ ///
+ /// Gets an instance of a service.
+ ///
+ /// The type of the service.
+ /// The factory.
+ /// An instance of the specified type.
+ /// Throws an exception if the factory failed to get an instance of the specified type.
+ public static T GetInstance(this IFactory factory)
+ => (T)factory.GetInstance(typeof(T));
+
+ ///
+ /// Tries to get an instance of a service.
+ ///
+ /// The type of the service.
+ /// An instance of the specified type, or null.
+ /// Returns null if the factory does not know how to get an instance
+ /// of the specified type. Throws an exception if the factory does know how
+ /// to get an instance of the specified type, but failed to do so.
+ public static T TryGetInstance(this IFactory factory)
+ => (T)factory.TryGetInstance(typeof(T));
+
+ ///
+ /// Creates an instance with arguments.
+ ///
+ /// The type of the instance.
+ /// The factory.
+ /// Arguments.
+ /// An instance of the specified type.
+ ///
+ /// Throws an exception if the factory failed to get an instance of the specified type.
+ /// The arguments are used as dependencies by the factory.
+ ///
+ public static T CreateInstance(this IFactory factory, params object[] args)
+ => (T)factory.CreateInstance(typeof(T), args);
+
+ ///
+ /// Creates an instance of a service, with arguments.
+ ///
+ ///
+ /// The type of the instance.
+ /// Named arguments.
+ /// An instance of the specified type.
+ ///
+ /// The instance type does not need to be registered into the factory.
+ /// The arguments are used as dependencies by the factory. Other dependencies
+ /// are retrieved from the factory.
+ ///
+ public static object CreateInstance(this IFactory factory, Type type, params object[] args)
+ {
+ // LightInject has this, but then it requires RegisterConstructorDependency etc and has various oddities
+ // including the most annoying one, which is that it does not work on singletons (hard to fix)
+ //return factory.GetInstance(type, args);
+
+ // this method is essentially used to build singleton instances, so it is assumed that it would be
+ // more expensive to build and cache a dynamic method ctor than to simply invoke the ctor, as we do
+ // here - this can be discussed
+
+ // TODO: we currently try the ctor with most parameters, but we could want to fall back to others
+
+ var ctor = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public).OrderByDescending(x => x.GetParameters().Length).FirstOrDefault();
+ if (ctor == null) throw new InvalidOperationException($"Could not find a public constructor for type {type.FullName}.");
+
+ var ctorParameters = ctor.GetParameters();
+ var ctorArgs = new object[ctorParameters.Length];
+ var i = 0;
+ foreach (var parameter in ctorParameters)
+ {
+ // no! IsInstanceOfType is not ok here
+ // ReSharper disable once UseMethodIsInstanceOfType
+ var arg = args?.FirstOrDefault(a => parameter.ParameterType.IsAssignableFrom(a.GetType()));
+ ctorArgs[i++] = arg ?? factory.GetInstance(parameter.ParameterType);
+ }
+ return ctor.Invoke(ctorArgs);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Umbraco.Core/Composing/IContainer.cs b/src/Umbraco.Core/Composing/IContainer.cs
deleted file mode 100644
index 22b6ac3eae..0000000000
--- a/src/Umbraco.Core/Composing/IContainer.cs
+++ /dev/null
@@ -1,149 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace Umbraco.Core.Composing
-{
- // Implementing IContainer:
- //
- // The factory
- // - always picks the constructor with the most parameters
- // - supports Lazy parameters (and prefers them over non-Lazy) in constructors
- // - what happens with 'releasing' is unclear
- //
- // The registry
- // - supports registering a service, even after some instances of other services have been created
- // - supports re-registering a service, as long as no instance of that service has been created
- // - throws when re-registering a service, and an instance of that service has been created
- //
- // - registers only one implementation of a nameless service, re-registering replaces the previous
- // registration - names are required to register multiple implementations - and getting an
- // IEnumerable of the service, nameless, returns them all
-
- ///
- /// Defines a service register for Umbraco.
- ///
- public interface IRegister
- {
- ///
- /// Gets the concrete container.
- ///
- object ConcreteContainer { get; }
-
- ///
- /// Registers a service as its own implementation.
- ///
- void Register(Type serviceType, Lifetime lifetime = Lifetime.Transient);
-
- ///
- /// Registers a service with an implementation type.
- ///
- void Register(Type serviceType, Type implementingType, Lifetime lifetime = Lifetime.Transient);
-
- ///
- /// Registers a service with an implementation factory.
- ///
- void Register(Func factory, Lifetime lifetime = Lifetime.Transient);
-
- ///
- /// Registers a service with an implementing instance.
- ///
- void RegisterInstance(Type serviceType, object instance);
-
- ///
- /// Registers a base type for auto-registration.
- ///
- ///
- /// Auto-registration means that anytime the container is asked to create an instance
- /// of a type deriving from , it will first register that
- /// type automatically.
- /// This can be used for instance for views or controllers. Then, one just needs to
- /// register a common base class or interface, and the container knows how to create instances.
- ///
- void RegisterAuto(Type serviceBaseType);
-
- #region Control
-
- ///
- /// Configures the container for web support.
- ///
- /// The container.
- ///
- /// Enables support for MVC, WebAPI, but *not* per-request scope. This is used early in the boot
- /// process, where anything "scoped" should not be linked to a web request.
- ///
- IContainer ConfigureForWeb();
-
- ///
- /// Enables per-request scope.
- ///
- /// The container.
- ///
- /// Ties scopes to web requests.
- ///
- IContainer EnablePerWebRequestScope();
-
- #endregion
- }
-
- ///
- /// Defines a service factory for Umbraco.
- ///
- public interface IFactory
- {
- ///
- /// Gets an instance of a service.
- ///
- /// The type of the service.
- /// An instance of the specified type.
- /// Throws an exception if the container failed to get an instance of the specified type.
- object GetInstance(Type type);
-
- ///
- /// Tries to get an instance of a service.
- ///
- /// The type of the service.
- /// An instance of the specified type, or null.
- /// Returns null if the container does not know how to get an instance
- /// of the specified type. Throws an exception if the container does know how
- /// to get an instance of the specified type, but failed to do so.
- object TryGetInstance(Type type);
-
- ///
- /// Gets all instances of a service.
- ///
- /// The type of the service.
- IEnumerable GetAllInstances(Type serviceType);
-
- ///
- /// Gets all instances of a service.
- ///
- /// The type of the service.
- IEnumerable GetAllInstances();
-
- ///
- /// Releases an instance.
- ///
- /// The instance.
- ///
- /// See https://stackoverflow.com/questions/14072208 and http://kozmic.net/2010/08/27/must-i-release-everything-when-using-windsor/,
- /// you only need to release instances you specifically resolved, and even then, if done right, that might never be needed. For
- /// instance, LightInject does not require this and does not support it - should work with scopes.
- ///
- void Release(object instance);
-
- ///
- /// Begins a scope.
- ///
- ///
- /// When the scope is disposed, scoped instances that have been created during the scope are disposed.
- /// Scopes can be nested. Each instance is disposed individually.
- ///
- IDisposable BeginScope();
- }
-
- ///
- /// Defines a container for Umbraco.
- ///
- public interface IContainer : IRegister, IFactory, IDisposable // fixme kill?
- { }
-}
diff --git a/src/Umbraco.Core/Composing/IFactory.cs b/src/Umbraco.Core/Composing/IFactory.cs
new file mode 100644
index 0000000000..3e8cdf08e0
--- /dev/null
+++ b/src/Umbraco.Core/Composing/IFactory.cs
@@ -0,0 +1,68 @@
+using System;
+using System.Collections.Generic;
+
+namespace Umbraco.Core.Composing
+{
+ // Implementing:
+ //
+ // The factory
+ // - always picks the constructor with the most parameters
+ // - supports Lazy parameters (and prefers them over non-Lazy) in constructors
+ // - what happens with 'releasing' is unclear
+
+ ///
+ /// Defines a service factory for Umbraco.
+ ///
+ public interface IFactory
+ {
+ ///
+ /// Gets an instance of a service.
+ ///
+ /// The type of the service.
+ /// An instance of the specified type.
+ /// Throws an exception if the container failed to get an instance of the specified type.
+ object GetInstance(Type type);
+
+ ///
+ /// Tries to get an instance of a service.
+ ///
+ /// The type of the service.
+ /// An instance of the specified type, or null.
+ /// Returns null if the container does not know how to get an instance
+ /// of the specified type. Throws an exception if the container does know how
+ /// to get an instance of the specified type, but failed to do so.
+ object TryGetInstance(Type type);
+
+ ///
+ /// Gets all instances of a service.
+ ///
+ /// The type of the service.
+ IEnumerable GetAllInstances(Type serviceType);
+
+ ///
+ /// Gets all instances of a service.
+ ///
+ /// The type of the service.
+ IEnumerable GetAllInstances();
+
+ ///
+ /// Releases an instance.
+ ///
+ /// The instance.
+ ///
+ /// See https://stackoverflow.com/questions/14072208 and http://kozmic.net/2010/08/27/must-i-release-everything-when-using-windsor/,
+ /// you only need to release instances you specifically resolved, and even then, if done right, that might never be needed. For
+ /// instance, LightInject does not require this and does not support it - should work with scopes.
+ ///
+ void Release(object instance);
+
+ ///
+ /// Begins a scope.
+ ///
+ ///
+ /// When the scope is disposed, scoped instances that have been created during the scope are disposed.
+ /// Scopes can be nested. Each instance is disposed individually.
+ ///
+ IDisposable BeginScope();
+ }
+}
diff --git a/src/Umbraco.Core/Composing/IRegister.cs b/src/Umbraco.Core/Composing/IRegister.cs
new file mode 100644
index 0000000000..1ce7ae9f0b
--- /dev/null
+++ b/src/Umbraco.Core/Composing/IRegister.cs
@@ -0,0 +1,84 @@
+using System;
+
+namespace Umbraco.Core.Composing
+{
+ // Implementing:
+ //
+ // The register
+ // - supports registering a service, even after some instances of other services have been created
+ // - supports re-registering a service, as long as no instance of that service has been created
+ // - throws when re-registering a service, and an instance of that service has been created
+ //
+ // - registers only one implementation of a nameless service, re-registering replaces the previous
+ // registration - names are required to register multiple implementations - and getting an
+ // IEnumerable of the service, nameless, returns them all
+
+ ///
+ /// Defines a service register for Umbraco.
+ ///
+ public interface IRegister
+ {
+ ///
+ /// Gets the concrete container.
+ ///
+ object ConcreteContainer { get; }
+
+ ///
+ /// Registers a service as its own implementation.
+ ///
+ void Register(Type serviceType, Lifetime lifetime = Lifetime.Transient);
+
+ ///
+ /// Registers a service with an implementation type.
+ ///
+ void Register(Type serviceType, Type implementingType, Lifetime lifetime = Lifetime.Transient);
+
+ ///
+ /// Registers a service with an implementation factory.
+ ///
+ void Register(Func factory, Lifetime lifetime = Lifetime.Transient);
+
+ ///
+ /// Registers a service with an implementing instance.
+ ///
+ void RegisterInstance(Type serviceType, object instance);
+
+ ///
+ /// Registers a base type for auto-registration.
+ ///
+ ///
+ /// Auto-registration means that anytime the container is asked to create an instance
+ /// of a type deriving from , it will first register that
+ /// type automatically.
+ /// This can be used for instance for views or controllers. Then, one just needs to
+ /// register a common base class or interface, and the container knows how to create instances.
+ ///
+ void RegisterAuto(Type serviceBaseType);
+
+ #region Control
+
+ ///
+ /// Configures the container for web support.
+ ///
+ ///
+ /// Enables support for MVC, WebAPI, but *not* per-request scope. This is used early in the boot
+ /// process, where anything "scoped" should not be linked to a web request.
+ ///
+ void ConfigureForWeb();
+
+ ///
+ /// Enables per-request scope.
+ ///
+ ///
+ /// Ties scopes to web requests.
+ ///
+ void EnablePerWebRequestScope();
+
+ ///
+ /// Creates the factory.
+ ///
+ IFactory CreateFactory();
+
+ #endregion
+ }
+}
diff --git a/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs b/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs
index a15bfdfb16..d94643e49a 100644
--- a/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs
+++ b/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs
@@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;
using System.Reflection;
-using System.Linq;
using System.Threading;
using LightInject;
namespace Umbraco.Core.Composing.LightInject
{
///
- /// Implements with LightInject.
+ /// Implements DI with LightInject.
///
- public class LightInjectContainer : IContainer
+ public class LightInjectContainer : IRegister, IFactory, IDisposable
{
private int _disposed;
@@ -99,6 +98,9 @@ namespace Umbraco.Core.Composing.LightInject
Container.Dispose();
}
+ ///
+ public IFactory CreateFactory() => this;
+
#region Factory
///
@@ -246,18 +248,15 @@ namespace Umbraco.Core.Composing.LightInject
=> Container.BeginScope();
///
- public virtual IContainer ConfigureForWeb()
- {
- return this;
- }
+ public virtual void ConfigureForWeb()
+ { }
///
- public IContainer EnablePerWebRequestScope()
+ public void EnablePerWebRequestScope()
{
if (!(Container.ScopeManagerProvider is MixedLightInjectScopeManagerProvider smp))
throw new Exception("Container.ScopeManagerProvider is not MixedLightInjectScopeManagerProvider.");
smp.EnablePerWebRequestScope();
- return this;
}
private class AssemblyScanner : IAssemblyScanner
diff --git a/src/Umbraco.Core/Composing/RegisterExtensions.cs b/src/Umbraco.Core/Composing/RegisterExtensions.cs
index fc6aa78f0b..cff51a7f53 100644
--- a/src/Umbraco.Core/Composing/RegisterExtensions.cs
+++ b/src/Umbraco.Core/Composing/RegisterExtensions.cs
@@ -1,171 +1,92 @@
using System;
-using System.Collections.Concurrent;
-using System.Collections.Generic;
-using System.Linq;
-using System.Reflection;
using Umbraco.Core.IO;
namespace Umbraco.Core.Composing
{
///
- /// Provides extension methods to the class.
+ /// Provides extension methods to the class.
///
public static class RegisterExtensions
{
- ///
- /// Gets an instance of a service.
- ///
- /// The type of the service.
- /// The container.
- /// An instance of the specified type.
- /// Throws an exception if the container failed to get an instance of the specified type.
- public static T GetInstance(this IFactory container)
- => (T) container.GetInstance(typeof(T));
-
- ///
- /// Tries to get an instance of a service.
- ///
- /// The type of the service.
- /// An instance of the specified type, or null.
- /// Returns null if the container does not know how to get an instance
- /// of the specified type. Throws an exception if the container does know how
- /// to get an instance of the specified type, but failed to do so.
- public static T TryGetInstance(this IFactory container)
- => (T) container.TryGetInstance(typeof(T));
-
- ///
- /// Creates an instance with arguments.
- ///
- /// The type of the instance.
- /// The container.
- /// Arguments.
- /// An instance of the specified type.
- ///
- /// Throws an exception if the container failed to get an instance of the specified type.
- /// The arguments are used as dependencies by the container.
- ///
- public static T CreateInstance(this IFactory container, params object[] args)
- => (T) container.CreateInstance(typeof(T), args);
-
///
/// Registers a service with an implementation type.
///
- public static void Register(this IRegister container, Lifetime lifetime = Lifetime.Transient)
- => container.Register(typeof(TService), typeof(TImplementing), lifetime);
+ public static void Register(this IRegister register, Lifetime lifetime = Lifetime.Transient)
+ => register.Register(typeof(TService), typeof(TImplementing), lifetime);
///
/// Registers a service as its own implementation.
///
- public static void Register(this IRegister container, Lifetime lifetime = Lifetime.Transient)
- => container.Register(typeof(TService), lifetime);
+ public static void Register(this IRegister register, Lifetime lifetime = Lifetime.Transient)
+ => register.Register(typeof(TService), lifetime);
///
/// Registers a singleton service as its own implementation.
///
- public static void RegisterSingleton(this IRegister container)
- => container.Register(typeof(TService), Lifetime.Singleton);
+ public static void RegisterSingleton(this IRegister register)
+ => register.Register(typeof(TService), Lifetime.Singleton);
///
/// Registers a singleton service with an implementation type.
///
- public static void RegisterSingleton(this IRegister container)
- => container.Register(typeof(TService), typeof(TImplementing), Lifetime.Singleton);
+ public static void RegisterSingleton(this IRegister register)
+ => register.Register(typeof(TService), typeof(TImplementing), Lifetime.Singleton);
///
/// Registers a singleton service with an implementation factory.
///
- public static void RegisterSingleton(this IRegister container, Func factory)
- => container.Register(factory, Lifetime.Singleton);
+ public static void RegisterSingleton(this IRegister register, Func factory)
+ => register.Register(factory, Lifetime.Singleton);
///
/// Registers a service with an implementing instance.
///
- public static void RegisterInstance(this IRegister container, TService instance)
- => container.RegisterInstance(typeof(TService), instance);
+ public static void RegisterInstance(this IRegister register, TService instance)
+ => register.RegisterInstance(typeof(TService), instance);
///
/// Registers a base type for auto-registration.
///
- public static void RegisterAuto(this IRegister container)
- => container.RegisterAuto(typeof(TServiceBase));
-
- ///
- /// Creates an instance of a service, with arguments.
- ///
- ///
- /// The type of the instance.
- /// Named arguments.
- /// An instance of the specified type.
- ///
- /// The instance type does not need to be registered into the container.
- /// The arguments are used as dependencies by the container. Other dependencies
- /// are retrieved from the container.
- ///
- public static object CreateInstance(this IFactory container, Type type, params object[] args)
- {
- // LightInject has this, but then it requires RegisterConstructorDependency etc and has various oddities
- // including the most annoying one, which is that it does not work on singletons (hard to fix)
- //return Container.GetInstance(type, args);
-
- // this method is essentially used to build singleton instances, so it is assumed that it would be
- // more expensive to build and cache a dynamic method ctor than to simply invoke the ctor, as we do
- // here - this can be discussed
-
- // TODO: we currently try the ctor with most parameters, but we could want to fall back to others
-
- var ctor = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public).OrderByDescending(x => x.GetParameters().Length).FirstOrDefault();
- if (ctor == null) throw new InvalidOperationException($"Could not find a public constructor for type {type.FullName}.");
-
- var ctorParameters = ctor.GetParameters();
- var ctorArgs = new object[ctorParameters.Length];
- var i = 0;
- foreach (var parameter in ctorParameters)
- {
- // no! IsInstanceOfType is not ok here
- // ReSharper disable once UseMethodIsInstanceOfType
- var arg = args?.FirstOrDefault(a => parameter.ParameterType.IsAssignableFrom(a.GetType()));
- ctorArgs[i++] = arg ?? container.GetInstance(parameter.ParameterType);
- }
- return ctor.Invoke(ctorArgs);
- }
+ public static void RegisterAuto(this IRegister register)
+ => register.RegisterAuto(typeof(TServiceBase));
///
/// Registers a filesystem.
///
/// The type of the filesystem.
/// The implementing type.
- /// The container.
+ /// The register.
/// A factory method creating the supporting filesystem.
- /// The container.
- public static IRegister RegisterFileSystem(this IRegister container, Func supportingFileSystemFactory)
+ /// The register.
+ public static IRegister RegisterFileSystem(this IRegister register, Func supportingFileSystemFactory)
where TImplementing : FileSystemWrapper, TFileSystem
{
- container.RegisterSingleton(factory =>
+ register.RegisterSingleton(factory =>
{
var fileSystems = factory.GetInstance();
return fileSystems.GetFileSystem(supportingFileSystemFactory(factory));
});
- return container;
+ return register;
}
///
/// Registers a filesystem.
///
/// The type of the filesystem.
- /// The container.
+ /// The register.
/// A factory method creating the supporting filesystem.
- /// The container.
- public static IRegister RegisterFileSystem(this IRegister container, Func supportingFileSystemFactory)
+ /// The register.
+ public static IRegister RegisterFileSystem(this IRegister register, Func supportingFileSystemFactory)
where TFileSystem : FileSystemWrapper
{
- container.RegisterSingleton(factory =>
+ register.RegisterSingleton(factory =>
{
var fileSystems = factory.GetInstance();
return fileSystems.GetFileSystem(supportingFileSystemFactory(factory));
});
- return container;
+ return register;
}
}
}
diff --git a/src/Umbraco.Core/Composing/ContainerFactory.cs b/src/Umbraco.Core/Composing/RegisterFactory.cs
similarity index 90%
rename from src/Umbraco.Core/Composing/ContainerFactory.cs
rename to src/Umbraco.Core/Composing/RegisterFactory.cs
index fb2c5f6eb7..bb132c247a 100644
--- a/src/Umbraco.Core/Composing/ContainerFactory.cs
+++ b/src/Umbraco.Core/Composing/RegisterFactory.cs
@@ -7,7 +7,7 @@ namespace Umbraco.Core.Composing
///
/// Creates the container.
///
- public static class ContainerFactory
+ public static class RegisterFactory
{
// 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
@@ -19,12 +19,14 @@ namespace Umbraco.Core.Composing
///
///
/// 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.
+ /// a fully qualified type name to a class with a static method "Create" returning an IRegister.
///
- public static IContainer Create()
+ public static IRegister Create()
{
Type type;
+ // fixme naming / container?
+
var configuredTypeName = ConfigurationManager.AppSettings["umbracoContainerType"];
if (configuredTypeName.IsNullOrWhiteSpace())
{
@@ -46,9 +48,9 @@ namespace Umbraco.Core.Composing
if (factoryMethod == null)
throw new Exception($"Container factory class '{configuredTypeName}' does not have a public static method named Create.");
- var container = factoryMethod.Invoke(null, Array.Empty()) as IContainer;
+ var container = factoryMethod.Invoke(null, Array.Empty()) as IRegister;
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 IRegister implementation.");
return container;
}
diff --git a/src/Umbraco.Core/IRuntime.cs b/src/Umbraco.Core/IRuntime.cs
index 12a189b781..8a62e76a5e 100644
--- a/src/Umbraco.Core/IRuntime.cs
+++ b/src/Umbraco.Core/IRuntime.cs
@@ -10,8 +10,8 @@ namespace Umbraco.Core
///
/// Boots the runtime.
///
- /// The application container.
- void Boot(IContainer container);
+ /// The application register.
+ void Boot(IRegister register);
///
/// Gets the runtime state.
diff --git a/src/Umbraco.Core/Runtime/CoreRuntime.cs b/src/Umbraco.Core/Runtime/CoreRuntime.cs
index 4793b1994b..9ccb45cf0f 100644
--- a/src/Umbraco.Core/Runtime/CoreRuntime.cs
+++ b/src/Umbraco.Core/Runtime/CoreRuntime.cs
@@ -27,8 +27,8 @@ namespace Umbraco.Core.Runtime
public class CoreRuntime : IRuntime
{
private Components.Components _components;
+ private IFactory _factory;
private RuntimeState _state;
- private IContainer _container;
///
/// Gets the logger.
@@ -44,22 +44,19 @@ namespace Umbraco.Core.Runtime
public IRuntimeState State => _state;
///
- public virtual void Boot(IContainer container)
+ public virtual void Boot(IRegister register)
{
- // assign current container
- Current.Factory = _container = container;
-
// create and register the essential services
// ie the bare minimum required to boot
// loggers
var logger = GetLogger();
- container.RegisterInstance(logger);
+ register.RegisterInstance(logger);
Logger = logger;
var profiler = GetProfiler();
- container.RegisterInstance(profiler);
+ register.RegisterInstance(profiler);
var profilingLogger = new ProfilingLogger(logger, profiler);
- container.RegisterInstance(profilingLogger);
+ register.RegisterInstance(profilingLogger);
ProfilingLogger = profilingLogger;
// application environment
@@ -69,32 +66,34 @@ namespace Umbraco.Core.Runtime
// application caches
var appCaches = GetAppCaches();
- container.RegisterInstance(appCaches);
+ register.RegisterInstance(appCaches);
var runtimeCache = appCaches.RuntimeCache;
- container.RegisterInstance(runtimeCache);
+ register.RegisterInstance(runtimeCache);
// database factory
var databaseFactory = GetDatabaseFactory();
- container.RegisterInstance(databaseFactory);
- container.RegisterSingleton(factory => databaseFactory.SqlContext);
+ register.RegisterInstance(databaseFactory);
+ register.RegisterSingleton(_ => databaseFactory.SqlContext);
// type loader
var globalSettings = UmbracoConfig.For.GlobalSettings();
var typeLoader = new TypeLoader(runtimeCache, globalSettings, profilingLogger);
- container.RegisterInstance(typeLoader);
+ register.RegisterInstance(typeLoader);
// runtime state
+ // beware! must use '() => _factory.GetInstance()' and NOT '_factory.GetInstance'
+ // as the second one captures the current value (null) and therefore fails
_state = new RuntimeState(logger,
UmbracoConfig.For.UmbracoSettings(), UmbracoConfig.For.GlobalSettings(),
- new Lazy(container.GetInstance),
- new Lazy(container.GetInstance))
+ new Lazy(() => _factory.GetInstance()),
+ new Lazy(() => _factory.GetInstance()))
{
Level = RuntimeLevel.Boot
};
- container.RegisterInstance(_state);
+ register.RegisterInstance(_state);
// create the composition
- var composition = new Composition(container, typeLoader, profilingLogger, RuntimeLevel.Boot);
+ var composition = new Composition(register, typeLoader, profilingLogger, RuntimeLevel.Boot);
// register runtime-level services
Compose(composition);
@@ -121,7 +120,7 @@ namespace Umbraco.Core.Runtime
logger.Debug("Runtime: {Runtime}", GetType().FullName);
var mainDom = AquireMainDom();
- container.RegisterInstance(mainDom);
+ register.RegisterInstance(mainDom);
DetermineRuntimeLevel(databaseFactory);
@@ -130,13 +129,9 @@ namespace Umbraco.Core.Runtime
_components.Compose();
- // fixme split Container into Register and Factory
- // we should have a Current.Factory not a Current.Container
- // we should compile the register into a factory *now*
- // using the factory before this point should just throw
- IFactory factory = container;
+ _factory = Current.Factory = register.CreateFactory();
- _components.Initialize(factory);
+ _components.Initialize(_factory);
}
catch (Exception e)
{
@@ -422,7 +417,7 @@ namespace Umbraco.Core.Runtime
///
/// This is strictly internal, for tests only.
protected internal virtual IUmbracoDatabaseFactory GetDatabaseFactory()
- => new UmbracoDatabaseFactory(Logger, new Lazy(_container.GetInstance));
+ => new UmbracoDatabaseFactory(Logger, new Lazy(() => _factory.GetInstance()));
#endregion
}
diff --git a/src/Umbraco.Core/RuntimeState.cs b/src/Umbraco.Core/RuntimeState.cs
index 4ba75ed89f..e98c67caeb 100644
--- a/src/Umbraco.Core/RuntimeState.cs
+++ b/src/Umbraco.Core/RuntimeState.cs
@@ -20,16 +20,13 @@ namespace Umbraco.Core
private readonly IUmbracoSettingsSection _settings;
private readonly IGlobalSettings _globalSettings;
private readonly HashSet _applicationUrls = new HashSet();
+ private readonly Lazy _mainDom;
+ private readonly Lazy _serverRegistrar;
private RuntimeLevel _level;
- private Lazy _mainDom;
- private Lazy _serverRegistrar;
///
/// Initializes a new instance of the class.
///
- /// A logger.
- /// Umbraco settings.
- /// Global settings.
public RuntimeState(ILogger logger, IUmbracoSettingsSection settings, IGlobalSettings globalSettings,
Lazy mainDom, Lazy serverRegistrar)
{
diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj
index 3dfde3b175..bc092271b7 100755
--- a/src/Umbraco.Core/Umbraco.Core.csproj
+++ b/src/Umbraco.Core/Umbraco.Core.csproj
@@ -177,13 +177,15 @@
-
+
+
-
+
+
diff --git a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs
index 2c68b1608a..5c2681e184 100644
--- a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs
+++ b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs
@@ -24,12 +24,12 @@ namespace Umbraco.Tests.Cache.DistributedCache
[SetUp]
public void Setup()
{
- var container = ContainerFactory.Create();
- Current.Factory = container;
- var composition = new Composition(container, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
+ var register = RegisterFactory.Create();
+ Current.Factory = register.CreateFactory(); // fixme only for LightInject
+ var composition = new Composition(register, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
- container.Register(_ => new TestServerRegistrar());
- container.RegisterSingleton(_ => new TestServerMessenger());
+ register.Register(_ => new TestServerRegistrar());
+ register.RegisterSingleton(_ => new TestServerMessenger());
composition.GetCollectionBuilder()
.Add();
diff --git a/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs b/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs
index 6047b5bb9a..f30c9a36e3 100644
--- a/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs
+++ b/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs
@@ -5,9 +5,7 @@ using Moq;
using NUnit.Framework;
using Umbraco.Core.Composing;
using Umbraco.Core;
-using Umbraco.Core.Cache;
using Umbraco.Core.Components;
-using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
namespace Umbraco.Tests.Composing
@@ -15,7 +13,7 @@ namespace Umbraco.Tests.Composing
[TestFixture]
public class CollectionBuildersTests
{
- private IContainer _container;
+ private IRegister _register;
private IFactory _factory;
private Composition _composition;
@@ -24,10 +22,10 @@ namespace Umbraco.Tests.Composing
{
Current.Reset();
- _container = ContainerFactory.Create();
- _factory = _container;
- Current.Factory = _container;
- _composition = new Composition(_container, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
+ _register = RegisterFactory.Create();
+ _factory = _register.CreateFactory(); // fixme only works w/LightInject - would require we reorg tests!
+ Current.Factory = _factory;
+ _composition = new Composition(_register, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
}
[TearDown]
@@ -35,8 +33,8 @@ namespace Umbraco.Tests.Composing
{
Current.Reset();
- _container.Dispose();
- _container = null;
+ _register.DisposeIfDisposable();
+ _register = null;
}
[Test]
@@ -301,10 +299,10 @@ namespace Umbraco.Tests.Composing
// but the container manages the scope, so to test the scope
// the collection must come from the container
- var col1 = _container.GetInstance();
+ var col1 = _factory.GetInstance();
AssertCollection(col1, typeof(Resolved1), typeof(Resolved2));
- var col2 = _container.GetInstance();
+ var col2 = _factory.GetInstance();
AssertCollection(col2, typeof(Resolved1), typeof(Resolved2));
AssertSameCollection(col1, col2);
@@ -321,10 +319,10 @@ namespace Umbraco.Tests.Composing
// but the container manages the scope, so to test the scope
// the collection must come from the container
- var col1 = _container.GetInstance();
+ var col1 = _factory.GetInstance();
AssertCollection(col1, typeof(Resolved1), typeof(Resolved2));
- var col2 = _container.GetInstance();
+ var col2 = _factory.GetInstance();
AssertCollection(col1, typeof(Resolved1), typeof(Resolved2));
AssertNotSameCollection(col1, col2);
@@ -355,10 +353,10 @@ namespace Umbraco.Tests.Composing
TestCollection col1A, col1B;
- using (_container.BeginScope())
+ using (_factory.BeginScope())
{
- col1A = _container.GetInstance();
- col1B = _container.GetInstance();
+ col1A = _factory.GetInstance();
+ col1B = _factory.GetInstance();
}
AssertCollection(col1A, typeof(Resolved1), typeof(Resolved2));
@@ -367,9 +365,9 @@ namespace Umbraco.Tests.Composing
TestCollection col2;
- using (_container.BeginScope())
+ using (_factory.BeginScope())
{
- col2 = _container.GetInstance();
+ col2 = _factory.GetInstance();
}
AssertCollection(col2, typeof(Resolved1), typeof(Resolved2));
diff --git a/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs b/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs
index 7b0ac4acf9..6e0fdd0912 100644
--- a/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs
+++ b/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs
@@ -11,45 +11,49 @@ namespace Umbraco.Tests.Composing
[TestFixture]
public class ContainerImplementationTests // FIXME merge into ContainerTests or ContainerConformingTests
{
- private IContainer CreateContainer() => ContainerFactory.Create();
+ private IRegister CreateRegister() => RegisterFactory.Create();
[Test]
public void CanRegisterSingletonInterface()
{
- var container = CreateContainer();
- container.RegisterSingleton();
- var s1 = container.GetInstance();
- var s2 = container.GetInstance();
+ var register = CreateRegister();
+ register.RegisterSingleton();
+ var factory = register.CreateFactory();
+ var s1 = factory.GetInstance();
+ var s2 = factory.GetInstance();
Assert.AreSame(s1, s2);
}
[Test]
public void CanRegisterSingletonClass()
{
- var container = CreateContainer();
- container.RegisterSingleton();
- var s1 = container.GetInstance();
- var s2 = container.GetInstance();
+ var register = CreateRegister();
+ register.RegisterSingleton();
+ var factory = register.CreateFactory();
+ var s1 = factory.GetInstance();
+ var s2 = factory.GetInstance();
Assert.AreSame(s1, s2);
}
[Test]
public void CanReRegisterSingletonInterface()
{
- var container = CreateContainer();
- container.RegisterSingleton();
- container.RegisterSingleton();
- var s = container.GetInstance();
+ var register = CreateRegister();
+ register.RegisterSingleton();
+ register.RegisterSingleton();
+ var factory = register.CreateFactory();
+ var s = factory.GetInstance();
Assert.IsInstanceOf(s);
}
[Test]
public void CanRegisterSingletonWithCreate()
{
- var container = CreateContainer();
- container.RegisterSingleton(c => c.CreateInstance(new TestClass1()));
- var s1 = container.GetInstance();
- var s2 = container.GetInstance();
+ var register = CreateRegister();
+ register.RegisterSingleton(c => c.CreateInstance(new TestClass1()));
+ var factory = register.CreateFactory();
+ var s1 = factory.GetInstance();
+ var s2 = factory.GetInstance();
Assert.AreSame(s1, s2);
}
diff --git a/src/Umbraco.Tests/Composing/ContainerTests.cs b/src/Umbraco.Tests/Composing/ContainerTests.cs
index 62080ef626..b5be97ce38 100644
--- a/src/Umbraco.Tests/Composing/ContainerTests.cs
+++ b/src/Umbraco.Tests/Composing/ContainerTests.cs
@@ -12,16 +12,18 @@ namespace Umbraco.Tests.Composing
{
// tests that a container conforms
- private IContainer GetContainer() => LightInjectContainer.Create();
+ private IRegister GetRegister() => LightInjectContainer.Create();
[Test]
public void CanRegisterAndGet()
{
- var container = GetContainer();
+ var register = GetRegister();
- container.Register();
+ register.Register();
- var thing = container.GetInstance();
+ var factory = register.CreateFactory();
+
+ var thing = factory.GetInstance();
Assert.IsNotNull(thing);
Assert.IsInstanceOf(thing);
}
@@ -29,11 +31,13 @@ namespace Umbraco.Tests.Composing
[Test]
public void CanRegisterAndGetLazy()
{
- var container = GetContainer();
+ var register = GetRegister();
- container.Register();
+ register.Register();
- var lazyThing = container.GetInstance>();
+ var factory = register.CreateFactory();
+
+ var lazyThing = factory.GetInstance>();
Assert.IsNotNull(lazyThing);
Assert.IsInstanceOf>(lazyThing);
var thing = lazyThing.Value;
@@ -44,31 +48,37 @@ namespace Umbraco.Tests.Composing
[Test]
public void CannotRegistedAndGetBase()
{
- var container = GetContainer();
+ var register = GetRegister();
- container.Register();
+ register.Register();
- Assert.IsNull(container.TryGetInstance());
+ var factory = register.CreateFactory();
+
+ Assert.IsNull(factory.TryGetInstance());
}
[Test]
public void CannotRegisterAndGetInterface()
{
- var container = GetContainer();
+ var register = GetRegister();
- container.Register();
+ register.Register();
- Assert.IsNull(container.TryGetInstance());
+ var factory = register.CreateFactory();
+
+ Assert.IsNull(factory.TryGetInstance());
}
[Test]
public void CanRegisterAndGetAllBase()
{
- var container = GetContainer();
+ var register = GetRegister();
- container.Register();
+ register.Register();
- var things = container.GetAllInstances();
+ var factory = register.CreateFactory();
+
+ var things = factory.GetAllInstances();
Assert.AreEqual(1, things.Count());
// lightInject: would be zero with option EnableVariance set to false
@@ -77,11 +87,13 @@ namespace Umbraco.Tests.Composing
[Test]
public void CanRegisterAndGetAllInterface()
{
- var container = GetContainer();
+ var register = GetRegister();
- container.Register();
+ register.Register();
- var things = container.GetAllInstances();
+ var factory = register.CreateFactory();
+
+ var things = factory.GetAllInstances();
Assert.AreEqual(1, things.Count());
// lightInject: would be zero with option EnableVariance set to false
@@ -90,11 +102,13 @@ namespace Umbraco.Tests.Composing
[Test]
public void CanRegisterBaseAndGet()
{
- var container = GetContainer();
+ var register = GetRegister();
- container.Register();
+ register.Register();
- var thing = container.GetInstance();
+ var factory = register.CreateFactory();
+
+ var thing = factory.GetInstance();
Assert.IsNotNull(thing);
Assert.IsInstanceOf(thing);
}
@@ -102,11 +116,13 @@ namespace Umbraco.Tests.Composing
[Test]
public void CanRegisterInterfaceAndGet()
{
- var container = GetContainer();
+ var register = GetRegister();
- container.Register();
+ register.Register();
- var thing = container.GetInstance();
+ var factory = register.CreateFactory();
+
+ var thing = factory.GetInstance();
Assert.IsNotNull(thing);
Assert.IsInstanceOf(thing);
}
@@ -114,21 +130,23 @@ namespace Umbraco.Tests.Composing
[Test]
public void NonSingletonServiceIsNotUnique()
{
- var container = GetContainer();
+ var register = GetRegister();
- container.Register();
- container.Register();
+ register.Register();
+ register.Register();
- var things = container.GetInstance>();
+ var factory = register.CreateFactory();
+
+ var things = factory.GetInstance>();
Assert.AreEqual(2, things.Count());
- Assert.IsNull(container.TryGetInstance());
+ Assert.IsNull(factory.TryGetInstance());
}
[Test]
public void SingletonServiceIsUnique()
{
- var container = GetContainer();
+ var register = GetRegister();
// for Core services that ppl may want to redefine in components,
// it is important to be able to have a unique, singleton implementation,
@@ -136,53 +154,61 @@ namespace Umbraco.Tests.Composing
// on each container
// redefine the service
- container.Register(Lifetime.Singleton);
- container.Register(Lifetime.Singleton);
+ register.Register(Lifetime.Singleton);
+ register.Register(Lifetime.Singleton);
- var things = container.GetInstance>();
+ var factory = register.CreateFactory();
+
+ var things = factory.GetInstance>();
Assert.AreEqual(1, things.Count());
- var thing = container.GetInstance();
+ var thing = factory.GetInstance();
Assert.IsInstanceOf(thing);
}
[Test]
public void SingletonImplementationIsNotUnique()
{
- var container = GetContainer();
+ var register = GetRegister();
// define two implementations
- container.Register(Lifetime.Singleton);
- container.Register(Lifetime.Singleton);
+ register.Register(Lifetime.Singleton);
+ register.Register(Lifetime.Singleton);
- var things = container.GetInstance>();
+ var factory = register.CreateFactory();
+
+ var things = factory.GetInstance>();
Assert.AreEqual(2, things.Count());
- Assert.IsNull(container.TryGetInstance());
+ Assert.IsNull(factory.TryGetInstance());
}
[Test]
public void CanInjectEnumerableOfBase()
{
- var container = GetContainer();
+ var register = GetRegister();
- container.Register();
- container.Register();
- container.Register();
+ register.Register();
+ register.Register();
+ register.Register();
- var needThings = container.GetInstance();
+ var factory = register.CreateFactory();
+
+ var needThings = factory.GetInstance();
Assert.AreEqual(2, needThings.Things.Count());
}
[Test]
public void CanGetEnumerableOfBase()
{
- var container = GetContainer();
+ var register = GetRegister();
- container.Register();
- container.Register();
+ register.Register();
+ register.Register();
- var things = container.GetInstance>();
+ var factory = register.CreateFactory();
+
+ var things = factory.GetInstance>();
Assert.AreEqual(2, things. Count());
}
diff --git a/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs b/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs
index c39b7d6943..b3009ae90f 100644
--- a/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs
+++ b/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs
@@ -27,11 +27,9 @@ namespace Umbraco.Tests.Composing
Current.Reset();
}
- private IContainer CreateContainer()
+ private IRegister CreateRegister()
{
- var container = ContainerFactory.Create();
- Current.Factory = container;
- return container;
+ return RegisterFactory.Create();
}
// note
@@ -41,7 +39,7 @@ namespace Umbraco.Tests.Composing
[Test]
public void LazyCollectionBuilderHandlesTypes()
{
- var container = CreateContainer();
+ var container = CreateRegister();
var composition = new Composition(container, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
composition.GetCollectionBuilder()
@@ -50,13 +48,15 @@ namespace Umbraco.Tests.Composing
.Add()
.Add();
- var values = container.GetInstance();
+ var factory = composition.CreateFactory();
+
+ var values = factory.GetInstance();
Assert.AreEqual(3, values.Count());
Assert.IsTrue(values.Select(x => x.GetType())
.ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) }));
- var other = container.GetInstance();
+ var other = factory.GetInstance();
Assert.AreNotSame(values, other); // transient
var o1 = other.FirstOrDefault(x => x is TransientObject1);
Assert.IsFalse(values.Contains(o1)); // transient
@@ -65,7 +65,7 @@ namespace Umbraco.Tests.Composing
[Test]
public void LazyCollectionBuilderHandlesProducers()
{
- var container = CreateContainer();
+ var container = CreateRegister();
var composition = new Composition(container, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
composition.GetCollectionBuilder()
@@ -73,13 +73,15 @@ namespace Umbraco.Tests.Composing
.Add(() => new[] { typeof(TransientObject3), typeof(TransientObject2) })
.Add(() => new[] { typeof(TransientObject1) });
- var values = container.GetInstance();
+ var factory = composition.CreateFactory();
+
+ var values = factory.GetInstance();
Assert.AreEqual(3, values.Count());
Assert.IsTrue(values.Select(x => x.GetType())
.ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) }));
- var other = container.GetInstance();
+ var other = factory.GetInstance();
Assert.AreNotSame(values, other); // transient
var o1 = other.FirstOrDefault(x => x is TransientObject1);
Assert.IsFalse(values.Contains(o1)); // transient
@@ -88,7 +90,7 @@ namespace Umbraco.Tests.Composing
[Test]
public void LazyCollectionBuilderHandlesTypesAndProducers()
{
- var container = CreateContainer();
+ var container = CreateRegister();
var composition = new Composition(container, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
composition.GetCollectionBuilder()
@@ -97,13 +99,15 @@ namespace Umbraco.Tests.Composing
.Add()
.Add(() => new[] { typeof(TransientObject1) });
- var values = container.GetInstance();
+ var factory = composition.CreateFactory();
+
+ var values = factory.GetInstance();
Assert.AreEqual(3, values.Count());
Assert.IsTrue(values.Select(x => x.GetType())
.ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) }));
- var other = container.GetInstance();
+ var other = factory.GetInstance();
Assert.AreNotSame(values, other); // transient
var o1 = other.FirstOrDefault(x => x is TransientObject1);
Assert.IsFalse(values.Contains(o1)); // transient
@@ -112,7 +116,7 @@ namespace Umbraco.Tests.Composing
[Test]
public void LazyCollectionBuilderThrowsOnIllegalTypes()
{
- var container = CreateContainer();
+ var container = CreateRegister();
var composition = new Composition(container, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
composition.GetCollectionBuilder()
@@ -124,17 +128,19 @@ namespace Umbraco.Tests.Composing
// legal so far...
.Add(() => new[] { typeof(TransientObject4) });
+ var factory = composition.CreateFactory();
+
Assert.Throws(() =>
{
// but throws here when trying to register the types
- var values = container.GetInstance();
+ var values = factory.GetInstance();
});
}
[Test]
public void LazyCollectionBuilderCanExcludeTypes()
{
- var container = CreateContainer();
+ var container = CreateRegister();
var composition = new Composition(container, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
composition.GetCollectionBuilder()
@@ -142,7 +148,9 @@ namespace Umbraco.Tests.Composing
.Add(() => new[] { typeof(TransientObject3), typeof(TransientObject2), typeof(TransientObject1) })
.Exclude();
- var values = container.GetInstance();
+ var factory = composition.CreateFactory();
+
+ var values = factory.GetInstance();
Assert.AreEqual(2, values.Count());
Assert.IsFalse(values.Select(x => x.GetType())
@@ -150,7 +158,7 @@ namespace Umbraco.Tests.Composing
Assert.IsTrue(values.Select(x => x.GetType())
.ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2) }));
- var other = container.GetInstance();
+ var other = factory.GetInstance();
Assert.AreNotSame(values, other); // transient
var o1 = other.FirstOrDefault(x => x is TransientObject1);
Assert.IsFalse(values.Contains(o1)); // transient
diff --git a/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs b/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs
index c130165d69..1b201f3bc5 100644
--- a/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs
+++ b/src/Umbraco.Tests/Composing/PackageActionCollectionTests.cs
@@ -17,8 +17,8 @@ namespace Umbraco.Tests.Composing
[Test]
public void PackageActionCollectionBuilderWorks()
{
- var container = ContainerFactory.Create();
- Current.Factory = container;
+ var container = RegisterFactory.Create();
+ Current.Factory = container.CreateFactory(); // fixme only for LightInject
var composition = new Composition(container, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
composition.GetCollectionBuilder()
diff --git a/src/Umbraco.Tests/CoreThings/TryConvertToTests.cs b/src/Umbraco.Tests/CoreThings/TryConvertToTests.cs
index 2e187d85a5..d997a1bebd 100644
--- a/src/Umbraco.Tests/CoreThings/TryConvertToTests.cs
+++ b/src/Umbraco.Tests/CoreThings/TryConvertToTests.cs
@@ -18,7 +18,7 @@ namespace Umbraco.Tests.CoreThings
var settings = SettingsForTests.GetDefaultUmbracoSettings();
// fixme - base should do it!
- Container.RegisterSingleton(_ => new DefaultShortStringHelper(settings));
+ Composition.RegisterSingleton(_ => new DefaultShortStringHelper(settings));
}
[Test]
diff --git a/src/Umbraco.Tests/IO/FileSystemsTests.cs b/src/Umbraco.Tests/IO/FileSystemsTests.cs
index cd2d60395f..be1964cd72 100644
--- a/src/Umbraco.Tests/IO/FileSystemsTests.cs
+++ b/src/Umbraco.Tests/IO/FileSystemsTests.cs
@@ -21,7 +21,8 @@ namespace Umbraco.Tests.IO
[TestFixture]
public class FileSystemsTests
{
- private IContainer _container;
+ private IRegister _register;
+ private IFactory _factory;
[SetUp]
public void Setup()
@@ -30,17 +31,19 @@ namespace Umbraco.Tests.IO
var config = SettingsForTests.GetDefaultUmbracoSettings();
SettingsForTests.ConfigureSettings(config);
- _container = ContainerFactory.Create();
- Current.Factory = _container;
- var composition = new Composition(_container, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
+ _register = RegisterFactory.Create();
- _container.Register(_ => Mock.Of());
- _container.Register(_ => Mock.Of());
- _container.Register(_ => Mock.Of());
- _container.RegisterSingleton();
+ var composition = new Composition(_register, new TypeLoader(), Mock.Of(), RuntimeLevel.Run);
+
+ composition.Register(_ => Mock.Of());
+ composition.Register(_ => Mock.Of());
+ composition.Register(_ => Mock.Of());
+ composition.RegisterSingleton();
composition.ComposeFileSystems();
+ _factory = composition.CreateFactory();
+
// make sure we start clean
// because some tests will create corrupt or weird filesystems
FileSystems.Reset();
@@ -53,37 +56,37 @@ namespace Umbraco.Tests.IO
FileSystems.Reset();
Current.Reset();
- _container.Dispose();
+ _register.DisposeIfDisposable();
}
- private FileSystems FileSystems => _container.GetInstance();
+ private FileSystems FileSystems => _factory.GetInstance();
[Test]
public void Can_Get_MediaFileSystem()
{
- var fileSystem = _container.GetInstance();
+ var fileSystem = _factory.GetInstance();
Assert.NotNull(fileSystem);
}
[Test]
public void Can_Get_IMediaFileSystem()
{
- var fileSystem = _container.GetInstance();
+ var fileSystem = _factory.GetInstance();
Assert.NotNull(fileSystem);
}
[Test]
public void IMediaFileSystem_Is_Singleton()
{
- var fileSystem1 = _container.GetInstance();
- var fileSystem2 = _container.GetInstance();
+ var fileSystem1 = _factory.GetInstance();
+ var fileSystem2 = _factory.GetInstance();
Assert.AreSame(fileSystem1, fileSystem2);
}
[Test]
public void Can_Delete_MediaFiles()
{
- var fs = _container.GetInstance();
+ var fs = _factory.GetInstance();
var ms = new MemoryStream(Encoding.UTF8.GetBytes("test"));
var virtPath = fs.GetMediaPath("file.txt", Guid.NewGuid(), Guid.NewGuid());
fs.AddFile(virtPath, ms);
diff --git a/src/Umbraco.Tests/Integration/ContentEventsTests.cs b/src/Umbraco.Tests/Integration/ContentEventsTests.cs
index 30dfabbbdc..df7bdb5ccf 100644
--- a/src/Umbraco.Tests/Integration/ContentEventsTests.cs
+++ b/src/Umbraco.Tests/Integration/ContentEventsTests.cs
@@ -49,8 +49,8 @@ namespace Umbraco.Tests.Integration
{
base.Compose();
- Container.Register(_ => new TestServerRegistrar()); // localhost-only
- Container.RegisterSingleton();
+ Composition.Register(_ => new TestServerRegistrar()); // localhost-only
+ Composition.RegisterSingleton();
Composition.GetCollectionBuilder()
.Add()
diff --git a/src/Umbraco.Tests/Issues/U9560.cs b/src/Umbraco.Tests/Issues/U9560.cs
index 830a1fb84d..e422cbc86b 100644
--- a/src/Umbraco.Tests/Issues/U9560.cs
+++ b/src/Umbraco.Tests/Issues/U9560.cs
@@ -27,7 +27,7 @@ namespace Umbraco.Tests.Issues
var aliasName = string.Empty;
// read fields, same as what we do with PetaPoco Fetch
- using (var db = Container.GetInstance().CreateDatabase())
+ using (var db = Factory.GetInstance().CreateDatabase())
{
db.OpenSharedConnection();
try
@@ -55,7 +55,7 @@ namespace Umbraco.Tests.Issues
Assert.AreEqual("Alias", aliasName);
// try differently
- using (var db = Container.GetInstance().CreateDatabase())
+ using (var db = Factory.GetInstance