Skip to content

Commit 13a5816

Browse files
committed
Adding decorator support
2 parents 118635a + fcee1e7 commit 13a5816

File tree

5 files changed

+124
-2
lines changed

5 files changed

+124
-2
lines changed

src/Ninject.Extensions.Factory.Test/FactoryTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,42 @@ public void GenericFactoryMethodsAreSupported()
305305
handlers.Single().Should().BeOfType<IntMessageHandler>();
306306
}
307307

308+
[Fact]
309+
public void DecoratorShouldBeInjectedWithAppropriateBinding()
310+
{
311+
this.kernel.Bind<IWeapon>().To<DecoratedWeapon>();
312+
this.kernel.Bind<IWeapon>().To<Sword>().WhenInjectedExactlyInto<DecoratedWeapon>();
313+
this.kernel.Bind<IWeaponFactory>().ToFactory();
314+
315+
var instance = this.kernel.Get<IWeaponFactory>().CreateWeapon();
316+
var decoratedWeapon = instance as DecoratedWeapon;
317+
318+
instance.Should().BeOfType<DecoratedWeapon>();
319+
decoratedWeapon.WeaponToBeDecorated.Should().BeOfType<Sword>();
320+
321+
}
322+
323+
[Fact]
324+
public void DecoratorShouldBeInjectedWithAppropriateBindingWithArguments()
325+
{
326+
const string Name = "Excalibur";
327+
const int Width = 34;
328+
const int Length = 123;
329+
330+
this.kernel.Bind<ICustomizableWeapon>().To<DecoratedCustomizableWeapon>();
331+
this.kernel.Bind<ICustomizableWeapon>().To<CustomizableSword>().WhenInjectedExactlyInto<DecoratedCustomizableWeapon>();
332+
this.kernel.Bind<IWeaponFactory>().ToFactory();
333+
334+
var weapon = this.kernel.Get<IWeaponFactory>().CreateCustomizableWeapon(Length, Name, Width);
335+
var decoratedWeapon = weapon as DecoratedCustomizableWeapon;
336+
337+
weapon.Should().BeOfType<DecoratedCustomizableWeapon>();
338+
decoratedWeapon.WeaponToBeDecorated.Should().BeOfType<CustomizableSword>();
339+
weapon.Name.Should().Be(Name);
340+
weapon.Length.Should().Be(Length);
341+
weapon.Width.Should().Be(Width);
342+
}
343+
308344
private class CustomInstanceProvider : StandardInstanceProvider
309345
{
310346
protected override string GetName(System.Reflection.MethodInfo methodInfo, object[] arguments)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//-------------------------------------------------------------------------------
2+
// <copyright file="Sword.cs" company="Ninject Project Contributors">
3+
// Copyright (c) 2009-2011 Ninject Project Contributors
4+
// Authors: Remo Gloor ([email protected])
5+
//
6+
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
7+
// you may not use this file except in compliance with one of the Licenses.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
// or
12+
// http://www.microsoft.com/opensource/licenses.mspx
13+
//
14+
// Unless required by applicable law or agreed to in writing, software
15+
// distributed under the License is distributed on an "AS IS" BASIS,
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
// See the License for the specific language governing permissions and
18+
// limitations under the License.
19+
// </copyright>
20+
//-------------------------------------------------------------------------------
21+
22+
namespace Ninject.Extensions.Factory.Fakes
23+
{
24+
public class DecoratedCustomizableWeapon : ICustomizableWeapon
25+
{
26+
public ICustomizableWeapon WeaponToBeDecorated { get; private set; }
27+
28+
public DecoratedCustomizableWeapon(ICustomizableWeapon weaponToBeDecorated)
29+
{
30+
WeaponToBeDecorated = weaponToBeDecorated;
31+
}
32+
33+
public string Name
34+
{
35+
get { return WeaponToBeDecorated.Name; }
36+
}
37+
38+
public int Width
39+
{
40+
get { return WeaponToBeDecorated.Width; }
41+
}
42+
43+
public int Length
44+
{
45+
get { return WeaponToBeDecorated.Length; }
46+
}
47+
}
48+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//-------------------------------------------------------------------------------
2+
// <copyright file="Sword.cs" company="Ninject Project Contributors">
3+
// Copyright (c) 2009-2011 Ninject Project Contributors
4+
// Authors: Remo Gloor ([email protected])
5+
//
6+
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
7+
// you may not use this file except in compliance with one of the Licenses.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
// or
12+
// http://www.microsoft.com/opensource/licenses.mspx
13+
//
14+
// Unless required by applicable law or agreed to in writing, software
15+
// distributed under the License is distributed on an "AS IS" BASIS,
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
// See the License for the specific language governing permissions and
18+
// limitations under the License.
19+
// </copyright>
20+
//-------------------------------------------------------------------------------
21+
22+
namespace Ninject.Extensions.Factory.Fakes
23+
{
24+
public class DecoratedWeapon : IWeapon
25+
{
26+
public IWeapon WeaponToBeDecorated { get; private set; }
27+
28+
public DecoratedWeapon(IWeapon weaponToBeDecorated)
29+
{
30+
WeaponToBeDecorated = weaponToBeDecorated;
31+
}
32+
33+
public string Name
34+
{
35+
get { return "decorated " + WeaponToBeDecorated.Name; }
36+
}
37+
}
38+
}

src/Ninject.Extensions.Factory/Factory/StandardInstanceProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected virtual IConstructorArgument[] GetConstructorArguments(MethodInfo meth
111111
var constructorArguments = new ConstructorArgument[parameters.Length];
112112
for (int i = 0; i < parameters.Length; i++)
113113
{
114-
constructorArguments[i] = new ConstructorArgument(parameters[i].Name, arguments[i]);
114+
constructorArguments[i] = new ConstructorArgument(parameters[i].Name, arguments[i], true);
115115
}
116116

117117
return constructorArguments;

src/Ninject.Extensions.Factory/Ninject.Extensions.Factory.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
<ItemGroup>
3939
<PackageReference Include="Castle.Core" Version="4.1.1" />
40-
<PackageReference Include="Ninject" Version="3.3.0" />
40+
<PackageReference Include="Ninject" Version="3.3.1" />
4141
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004">
4242
<PrivateAssets>All</PrivateAssets>
4343
</PackageReference>

0 commit comments

Comments
 (0)