Skip to content

Commit

Permalink
added binding syntax extension method for defining a named binding co…
Browse files Browse the repository at this point in the history
…rresponding with a GetXYZ factory method
  • Loading branch information
ursenzler committed May 21, 2012
1 parent 9585049 commit b9d0b65
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Ninject.Extensions.Factory.Test/FactoryTests.cs
Expand Up @@ -82,6 +82,25 @@ public void NamedBinding()
weapon.Should().BeOfType<Sword>();
}

[Fact]
public void NamedLikeFactoryMethod()
{
this.kernel.Bind<IWeapon>().To<Sword>().NamedLikeFactoryMethod((IWeaponFactory f) => f.GetSword());
this.kernel.Bind<IWeaponFactory>().ToFactory();

var weapon = this.kernel.Get<IWeaponFactory>().GetSword();

weapon.Should().BeOfType<Sword>();
}

[Fact]
public void NamedLikeFactoryMethodThrowsExceptionWhenNotAGetFactoryMethod()
{
Action action = () => this.kernel.Bind<IWeapon>().To<Sword>().NamedLikeFactoryMethod((IWeaponFactory f) => f.CreateWeapon());

action.ShouldThrow<ArgumentException>();
}

[Fact]
public void GetFallback()
{
Expand Down Expand Up @@ -250,7 +269,7 @@ public void GenericFactoryMethodsAreSupported()
handlers.Should().HaveCount(1);
handlers.Single().Should().BeOfType<IntMessageHandler>();
}

private class CustomInstanceProvider : StandardInstanceProvider
{
protected override string GetName(System.Reflection.MethodInfo methodInfo, object[] arguments)
Expand Down
33 changes: 33 additions & 0 deletions src/Ninject.Extensions.Factory/BindToExtensions.cs
Expand Up @@ -23,6 +23,8 @@
namespace Ninject.Extensions.Factory
{
using System;
using System.Linq.Expressions;

using Castle.DynamicProxy;
using Ninject.Activation;
using Ninject.Syntax;
Expand Down Expand Up @@ -86,6 +88,37 @@ public static IBindingWhenInNamedWithOrOnSyntax<object> ToFactory(this IBindingT
return ToFactory(syntax, ctx => instanceProvider(), factoryType);
}

/// <summary>
/// Defines a named binding with the name taken from the factory method used to create instances.
/// </summary>
/// <typeparam name="TInterface">The type of the interface.</typeparam>
/// <typeparam name="TFactory">¨The type of the factory.</typeparam>
/// <param name="syntax">The syntax.</param>
/// <param name="action">Expression defining the factory method used to get the binding name from.</param>
/// <returns>
/// The <see cref="IBindingWithOrOnSyntax{TInterface}"/> to configure more things for the binding.
/// </returns>
public static IBindingWithOrOnSyntax<TInterface> NamedLikeFactoryMethod<TInterface, TFactory>(this IBindingNamedSyntax<TInterface> syntax, Expression<Action<TFactory>> action)
{
var methodCallExpression = action.Body as MethodCallExpression;

if (methodCallExpression == null)
{
throw new ArgumentException("expected factory method instead of " + action, "action");
}

var methodName = methodCallExpression.Method.Name;

if (!methodName.StartsWith("Get"))
{
throw new ArgumentException("expected factory 'Get' method instead of " + action, "action");
}

var bindingName = methodName.Substring(3);

return syntax.Named(bindingName);
}

/// <summary>
/// Defines that the interface shall be bound to an automatically created factory proxy.
/// </summary>
Expand Down

0 comments on commit b9d0b65

Please sign in to comment.