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/Factory/StandardInstanceProvider.cs b/src/Ninject.Extensions.Factory/Factory/StandardInstanceProvider.cs index a175036..0cd114e 100644 --- a/src/Ninject.Extensions.Factory/Factory/StandardInstanceProvider.cs +++ b/src/Ninject.Extensions.Factory/Factory/StandardInstanceProvider.cs @@ -111,7 +111,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; diff --git a/src/Ninject.Extensions.Factory/Ninject.Extensions.Factory.csproj b/src/Ninject.Extensions.Factory/Ninject.Extensions.Factory.csproj index b3566db..c6cd619 100644 --- a/src/Ninject.Extensions.Factory/Ninject.Extensions.Factory.csproj +++ b/src/Ninject.Extensions.Factory/Ninject.Extensions.Factory.csproj @@ -37,7 +37,7 @@ - + All