Skip to content

Commit b9d0b65

Browse files
committed
added binding syntax extension method for defining a named binding corresponding with a GetXYZ factory method
1 parent 9585049 commit b9d0b65

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ public void NamedBinding()
8282
weapon.Should().BeOfType<Sword>();
8383
}
8484

85+
[Fact]
86+
public void NamedLikeFactoryMethod()
87+
{
88+
this.kernel.Bind<IWeapon>().To<Sword>().NamedLikeFactoryMethod((IWeaponFactory f) => f.GetSword());
89+
this.kernel.Bind<IWeaponFactory>().ToFactory();
90+
91+
var weapon = this.kernel.Get<IWeaponFactory>().GetSword();
92+
93+
weapon.Should().BeOfType<Sword>();
94+
}
95+
96+
[Fact]
97+
public void NamedLikeFactoryMethodThrowsExceptionWhenNotAGetFactoryMethod()
98+
{
99+
Action action = () => this.kernel.Bind<IWeapon>().To<Sword>().NamedLikeFactoryMethod((IWeaponFactory f) => f.CreateWeapon());
100+
101+
action.ShouldThrow<ArgumentException>();
102+
}
103+
85104
[Fact]
86105
public void GetFallback()
87106
{
@@ -250,7 +269,7 @@ public void GenericFactoryMethodsAreSupported()
250269
handlers.Should().HaveCount(1);
251270
handlers.Single().Should().BeOfType<IntMessageHandler>();
252271
}
253-
272+
254273
private class CustomInstanceProvider : StandardInstanceProvider
255274
{
256275
protected override string GetName(System.Reflection.MethodInfo methodInfo, object[] arguments)

src/Ninject.Extensions.Factory/BindToExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
namespace Ninject.Extensions.Factory
2424
{
2525
using System;
26+
using System.Linq.Expressions;
27+
2628
using Castle.DynamicProxy;
2729
using Ninject.Activation;
2830
using Ninject.Syntax;
@@ -86,6 +88,37 @@ public static IBindingWhenInNamedWithOrOnSyntax<object> ToFactory(this IBindingT
8688
return ToFactory(syntax, ctx => instanceProvider(), factoryType);
8789
}
8890

91+
/// <summary>
92+
/// Defines a named binding with the name taken from the factory method used to create instances.
93+
/// </summary>
94+
/// <typeparam name="TInterface">The type of the interface.</typeparam>
95+
/// <typeparam name="TFactory">¨The type of the factory.</typeparam>
96+
/// <param name="syntax">The syntax.</param>
97+
/// <param name="action">Expression defining the factory method used to get the binding name from.</param>
98+
/// <returns>
99+
/// The <see cref="IBindingWithOrOnSyntax{TInterface}"/> to configure more things for the binding.
100+
/// </returns>
101+
public static IBindingWithOrOnSyntax<TInterface> NamedLikeFactoryMethod<TInterface, TFactory>(this IBindingNamedSyntax<TInterface> syntax, Expression<Action<TFactory>> action)
102+
{
103+
var methodCallExpression = action.Body as MethodCallExpression;
104+
105+
if (methodCallExpression == null)
106+
{
107+
throw new ArgumentException("expected factory method instead of " + action, "action");
108+
}
109+
110+
var methodName = methodCallExpression.Method.Name;
111+
112+
if (!methodName.StartsWith("Get"))
113+
{
114+
throw new ArgumentException("expected factory 'Get' method instead of " + action, "action");
115+
}
116+
117+
var bindingName = methodName.Substring(3);
118+
119+
return syntax.Named(bindingName);
120+
}
121+
89122
/// <summary>
90123
/// Defines that the interface shall be bound to an automatically created factory proxy.
91124
/// </summary>

0 commit comments

Comments
 (0)