Skip to content

Instantly share code, notes, and snippets.

@orient-man
Created January 13, 2014 13:40
Show Gist options
  • Save orient-man/8400383 to your computer and use it in GitHub Desktop.
Save orient-man/8400383 to your computer and use it in GitHub Desktop.
Bug: wrong interceptor is chosen when multiple bindings exist
using NUnit.Framework;
using Ninject;
using Ninject.Extensions.Interception;
using Ninject.Extensions.Interception.Infrastructure.Language;
namespace NInjectInterceptionTest
{
public class Program
{
public interface IFoo
{
int Run();
}
public class Foo1 : IFoo
{
public int Run()
{
return 1;
}
}
public class Foo2 : IFoo
{
public int Run()
{
return 2;
}
}
public class MultiplyBy10Interceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
invocation.ReturnValue = (int)invocation.ReturnValue*10;
}
}
public class MultiplyBy100Interceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
invocation.ReturnValue = (int)invocation.ReturnValue*100;
}
}
static void Main(string[] args)
{
var kernel = new StandardKernel();
kernel.Bind<IFoo>().To<Foo1>().Named("1");
kernel.Bind<IFoo>().To<Foo2>().Named("2");
kernel.Intercept(ctx => ctx.Plan.Type == typeof(Foo1)).With<MultiplyBy10Interceptor>();
kernel.Intercept(ctx => ctx.Plan.Type == typeof(Foo2)).With<MultiplyBy100Interceptor>();
var foo1 = kernel.Get<IFoo>(ctx => ctx.Name == "1");
var foo2 = kernel.Get<IFoo>(ctx => ctx.Name == "2");
Assert.That(foo1.Run(), Is.EqualTo(10));
// fails: returns 20
Assert.That(foo2.Run(), Is.EqualTo(200));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment