Created
April 6, 2011 16:04
-
-
Save Talljoe/905932 to your computer and use it in GitHub Desktop.
Ninject ambiguous test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Ninject; | |
using Ninject.Planning.Bindings; | |
namespace NinjectExamples | |
{ | |
using Xunit; | |
using Xunit.Should; | |
public class Chapter_9_ConstraintAttribute_Bindings | |
{ | |
[Fact] | |
public void CanUseContraintAttributes() | |
{ | |
var kernel = new StandardKernel(); | |
kernel.Bind<IWarrior>().To<Ninja>(); | |
kernel.Bind<IWarrior>().To<Samurai>().WithMetadata("CanSwim", false); | |
kernel.Bind<IWarrior>().To<SpecialNinja>().WithMetadata("CanSwim", true); | |
kernel.Get<AmbiguiousAttack>().Warrior.ShouldBeInstanceOf<SpecialNinja>(); | |
} | |
[Fact] | |
public void IfNoAttributeProvidedWillResolveToFirstBinding() | |
{ | |
var kernel = new StandardKernel(); | |
kernel.Bind<IWarrior>().To<Samurai>().WithMetadata("CanSwim", false); | |
kernel.Bind<IWarrior>().To<SpecialNinja>().WithMetadata("CanSwim", true); | |
kernel.Get<AmbiguiousAttack>().Warrior.ShouldBeInstanceOf<Samurai>(); | |
} | |
public interface IWarrior { } | |
public class Ninja : IWarrior { } | |
public class Samurai : IWarrior { } | |
public class SpecialNinja : IWarrior { } | |
private class AmbiguiousAttack | |
{ | |
public IWarrior Warrior; | |
public AmbiguiousAttack(IWarrior warrior) | |
{ | |
Warrior = warrior; | |
} | |
} | |
private class AmphibiousAttack | |
{ | |
public IWarrior Warrior; | |
public AmphibiousAttack([Swimmer]IWarrior warrior) | |
{ | |
Warrior = warrior; | |
} | |
} | |
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = true, Inherited = true)] | |
private class Swimmer : ConstraintAttribute | |
{ | |
public override bool Matches(IBindingMetadata metadata) | |
{ | |
return metadata.Has("CanSwim") && metadata.Get<bool>("CanSwim"); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Chapter_9_ConstraintAttribute_Bindings.IfNoAttributeProvidedWillResolveToFirstBinding : FailedNinject.ActivationException: Error activating IWarrior | |
More than one matching bindings are available. | |
Activation path: | |
2) Injection of dependency IWarrior into parameter warrior of constructor of type AmbiguiousAttack | |
1) Request for AmbiguiousAttack | |
Suggestions: | |
1) Ensure that you have defined a binding for IWarrior only once. | |
at Ninject.KernelBase.Resolve(IRequest request) in KernelBase.cs: line 367 | |
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) in Target.cs: line 197 | |
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in Target.cs: line 165 | |
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in StandardProvider.cs: line 97 | |
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass2.<Create>b__1(ITarget target) in StandardProvider.cs: line 81 | |
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() | |
at System.Linq.Buffer`1..ctor(IEnumerable`1 source) | |
at System.Linq.Enumerable.ToArray(IEnumerable`1 source) | |
at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in StandardProvider.cs: line 81 | |
at Ninject.Activation.Context.Resolve() in Context.cs: line 157 | |
at Ninject.KernelBase.<>c__DisplayClass8.<Resolve>b__6(IBinding binding) in KernelBase.cs: line 367 | |
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() | |
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext() | |
at System.Linq.Enumerable.Single(IEnumerable`1 source) | |
at Ninject.ResolutionExtensions.Get(IResolutionRoot root, IParameter[] parameters) in ResolutionExtensions.cs: line 37 | |
at NinjectExamples.Chapter_9_ConstraintAttribute_Bindings.IfNoAttributeProvidedWillResolveToFirstBinding() in Foo.cs: line 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment