From fcee1e72d58ee3db37d002f55be1f2e669bd06f9 Mon Sep 17 00:00:00 2001 From: TheOneSock Date: Fri, 1 Apr 2016 23:34:56 +0200 Subject: [PATCH] When creating a decorater pattern, constructor arguments should be propagated to the "object to be decorated" --- .../FactoryTests.cs | 36 ++++++++++++++ .../Fakes/DecoratedCustomizableWeapon.cs | 48 +++++++++++++++++++ .../Fakes/DecoratedWeapon.cs | 38 +++++++++++++++ .../Ninject.Extensions.Factory.Test.csproj | 2 + .../Factory/StandardInstanceProvider.cs | 2 +- 5 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/Ninject.Extensions.Factory.Test/Fakes/DecoratedCustomizableWeapon.cs create mode 100644 src/Ninject.Extensions.Factory.Test/Fakes/DecoratedWeapon.cs diff --git a/src/Ninject.Extensions.Factory.Test/FactoryTests.cs b/src/Ninject.Extensions.Factory.Test/FactoryTests.cs index 91a37ec..c8c031d 100644 --- a/src/Ninject.Extensions.Factory.Test/FactoryTests.cs +++ b/src/Ninject.Extensions.Factory.Test/FactoryTests.cs @@ -305,6 +305,42 @@ public void GenericFactoryMethodsAreSupported() handlers.Single().Should().BeOfType(); } + [Fact] + public void DecoratorShouldBeInjectedWithAppropriateBinding() + { + this.kernel.Bind().To(); + this.kernel.Bind().To().WhenInjectedExactlyInto(); + this.kernel.Bind().ToFactory(); + + var instance = this.kernel.Get().CreateWeapon(); + var decoratedWeapon = instance as DecoratedWeapon; + + instance.Should().BeOfType(); + decoratedWeapon.WeaponToBeDecorated.Should().BeOfType(); + + } + + [Fact] + public void DecoratorShouldBeInjectedWithAppropriateBindingWithArguments() + { + const string Name = "Excalibur"; + const int Width = 34; + const int Length = 123; + + this.kernel.Bind().To(); + this.kernel.Bind().To().WhenInjectedExactlyInto(); + this.kernel.Bind().ToFactory(); + + var weapon = this.kernel.Get().CreateCustomizableWeapon(Length, Name, Width); + var decoratedWeapon = weapon as DecoratedCustomizableWeapon; + + weapon.Should().BeOfType(); + decoratedWeapon.WeaponToBeDecorated.Should().BeOfType(); + weapon.Name.Should().Be(Name); + weapon.Length.Should().Be(Length); + weapon.Width.Should().Be(Width); + } + private class CustomInstanceProvider : StandardInstanceProvider { protected override string GetName(System.Reflection.MethodInfo methodInfo, object[] arguments) diff --git a/src/Ninject.Extensions.Factory.Test/Fakes/DecoratedCustomizableWeapon.cs b/src/Ninject.Extensions.Factory.Test/Fakes/DecoratedCustomizableWeapon.cs new file mode 100644 index 0000000..6df78c7 --- /dev/null +++ b/src/Ninject.Extensions.Factory.Test/Fakes/DecoratedCustomizableWeapon.cs @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------- +// +// Copyright (c) 2009-2011 Ninject Project Contributors +// Authors: Remo Gloor (remo.gloor@gmail.com) +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// you may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//------------------------------------------------------------------------------- + +namespace Ninject.Extensions.Factory.Fakes +{ + public class DecoratedCustomizableWeapon : ICustomizableWeapon + { + public ICustomizableWeapon WeaponToBeDecorated { get; private set; } + + public DecoratedCustomizableWeapon(ICustomizableWeapon weaponToBeDecorated) + { + WeaponToBeDecorated = weaponToBeDecorated; + } + + public string Name + { + get { return WeaponToBeDecorated.Name; } + } + + public int Width + { + get { return WeaponToBeDecorated.Width; } + } + + public int Length + { + get { return WeaponToBeDecorated.Length; } + } + } +} \ No newline at end of file diff --git a/src/Ninject.Extensions.Factory.Test/Fakes/DecoratedWeapon.cs b/src/Ninject.Extensions.Factory.Test/Fakes/DecoratedWeapon.cs new file mode 100644 index 0000000..ec23890 --- /dev/null +++ b/src/Ninject.Extensions.Factory.Test/Fakes/DecoratedWeapon.cs @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------------- +// +// Copyright (c) 2009-2011 Ninject Project Contributors +// Authors: Remo Gloor (remo.gloor@gmail.com) +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// you may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//------------------------------------------------------------------------------- + +namespace Ninject.Extensions.Factory.Fakes +{ + public class DecoratedWeapon : IWeapon + { + public IWeapon WeaponToBeDecorated { get; private set; } + + public DecoratedWeapon(IWeapon weaponToBeDecorated) + { + WeaponToBeDecorated = weaponToBeDecorated; + } + + public string Name + { + get { return "decorated " + WeaponToBeDecorated.Name; } + } + } +} \ No newline at end of file diff --git a/src/Ninject.Extensions.Factory.Test/Ninject.Extensions.Factory.Test.csproj b/src/Ninject.Extensions.Factory.Test/Ninject.Extensions.Factory.Test.csproj index 8cc1d1f..9fb161d 100644 --- a/src/Ninject.Extensions.Factory.Test/Ninject.Extensions.Factory.Test.csproj +++ b/src/Ninject.Extensions.Factory.Test/Ninject.Extensions.Factory.Test.csproj @@ -95,6 +95,8 @@ Properties\SharedAssemblyInfo.cs + + diff --git a/src/Ninject.Extensions.Factory/Factory/StandardInstanceProvider.cs b/src/Ninject.Extensions.Factory/Factory/StandardInstanceProvider.cs index dd47f06..209d50e 100644 --- a/src/Ninject.Extensions.Factory/Factory/StandardInstanceProvider.cs +++ b/src/Ninject.Extensions.Factory/Factory/StandardInstanceProvider.cs @@ -125,7 +125,7 @@ protected virtual IConstructorArgument[] GetConstructorArguments(MethodInfo meth var constructorArguments = new ConstructorArgument[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { - constructorArguments[i] = new ConstructorArgument(parameters[i].Name, arguments[i]); + constructorArguments[i] = new ConstructorArgument(parameters[i].Name, arguments[i], true); } return constructorArguments;