diff --git a/src/Umbraco.Core/Composing/Composers/FileSystemsComposer.cs b/src/Umbraco.Core/Composing/Composers/FileSystemsComposer.cs index a240f1c0ce..313e4ce97c 100644 --- a/src/Umbraco.Core/Composing/Composers/FileSystemsComposer.cs +++ b/src/Umbraco.Core/Composing/Composers/FileSystemsComposer.cs @@ -32,7 +32,6 @@ namespace Umbraco.Core.Composing.Composers * { } * } * - * Note that the ctor parameter MUST be named innerFileSystem. fixme oh yea? * The ctor can have more parameters that will be resolved by the container. * * Register your filesystem, in a component: @@ -74,7 +73,7 @@ namespace Umbraco.Core.Composing.Composers // it needs to be registered (not only the interface) because it provides additional // functionality eg for scoping, and is injected in the scope provider - whereas the // interface is really for end-users to get access to filesystems. - container.RegisterSingleton(factory => factory.CreateInstance(new { container} )); + container.RegisterSingleton(factory => factory.CreateInstance(container)); // register IFileSystems, which gives access too all filesystems container.RegisterSingleton(factory => factory.GetInstance()); diff --git a/src/Umbraco.Core/Composing/ContainerExtensions.cs b/src/Umbraco.Core/Composing/ContainerExtensions.cs index c9b3200b34..a2eb3af7cf 100644 --- a/src/Umbraco.Core/Composing/ContainerExtensions.cs +++ b/src/Umbraco.Core/Composing/ContainerExtensions.cs @@ -12,9 +12,6 @@ namespace Umbraco.Core.Composing /// public static class ContainerExtensions { - private static readonly ConcurrentDictionary>> ArgumentPropertyGetters - = new ConcurrentDictionary>>(); - /// /// Gets an instance of a service. /// @@ -55,35 +52,9 @@ namespace Umbraco.Core.Composing /// 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 IContainer container, IDictionary args) + public static T CreateInstance(this IContainer container, params object[] args) => (T) container.CreateInstance(typeof(T), args); - /// - /// 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 IContainer container, object args) - { - var typeOfArgs = args.GetType(); - var getters = ArgumentPropertyGetters.GetOrAdd(typeOfArgs, type => - args.GetType() - .GetProperties() - .ToDictionary(x => x.Name, x => ReflectionUtilities.EmitMethodUnsafe>(x.GetMethod))); - - var argsDictionary = new Dictionary(); - foreach (var (name, getter) in getters) - argsDictionary[name] = getter(args); - - return (T) container.CreateInstance(typeof(T), argsDictionary); - } - /// /// Registers a service with an implementation type. /// @@ -140,7 +111,7 @@ namespace Umbraco.Core.Composing // register the builder // use a factory so we don't have to self-register the container - container.RegisterSingleton(factory => factory.CreateInstance(new Dictionary {{ "container", container }} )); + container.RegisterSingleton(factory => factory.CreateInstance(container)); // initialize and return the builder return container.GetInstance(); @@ -158,7 +129,7 @@ namespace Umbraco.Core.Composing /// The arguments are used as dependencies by the container. Other dependencies /// are retrieved from the container. /// - public static object CreateInstance(this IContainer container, Type type, IDictionary args) + public static object CreateInstance(this IContainer 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) @@ -180,8 +151,7 @@ namespace Umbraco.Core.Composing { // no! IsInstanceOfType is not ok here // ReSharper disable once UseMethodIsInstanceOfType - // fixme so we just ignore the names? - var arg = args?.Values.FirstOrDefault(a => parameter.ParameterType.IsAssignableFrom(a.GetType())); + var arg = args?.FirstOrDefault(a => parameter.ParameterType.IsAssignableFrom(a.GetType())); ctorArgs[i++] = arg ?? container.GetInstance(parameter.ParameterType); } return ctor.Invoke(ctorArgs); diff --git a/src/Umbraco.Core/IO/FileSystems.cs b/src/Umbraco.Core/IO/FileSystems.cs index 4962d88a4e..47e2b02bb7 100644 --- a/src/Umbraco.Core/IO/FileSystems.cs +++ b/src/Umbraco.Core/IO/FileSystems.cs @@ -180,7 +180,7 @@ namespace Umbraco.Core.IO if (name == null) throw new Exception("panic!"); var shadowWrapper = CreateShadowWrapper(supporting, "typed/" + name); - return _container.CreateInstance(new { innerFileSystem = shadowWrapper }); + return _container.CreateInstance(shadowWrapper); })).Value; } diff --git a/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs b/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs index f16c6534fc..5b72bc17b4 100644 --- a/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs +++ b/src/Umbraco.Tests/Composing/ContainerImplementationTests.cs @@ -47,7 +47,7 @@ namespace Umbraco.Tests.Composing public void CanRegisterSingletonWithCreate() { var container = CreateContainer(); - container.RegisterSingleton(c => c.CreateInstance(new Dictionary{{"c", new TestClass1()}})); + container.RegisterSingleton(c => c.CreateInstance(new TestClass1())); var s1 = container.GetInstance(); var s2 = container.GetInstance(); Assert.AreSame(s1, s2);