From a003dea84db40894a743ba1c30367f3ca7677f8d Mon Sep 17 00:00:00 2001 From: Scott Xu Date: Sun, 17 Sep 2017 21:49:51 +0800 Subject: [PATCH] Fixed https://github.com/ninject/Ninject/issues/237 --- ReleaseNotes.md | 1 + .../Integration/ConstructorSelectionTests.cs | 13 +++++++++++++ .../Heuristics/StandardConstructorScorer.cs | 4 +++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ReleaseNotes.md b/ReleaseNotes.md index b246d1c..b4bbdde 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -17,6 +17,7 @@ Version 4.0 - Bugfix: Break Singleton / circular dependency WithPropertyValue - Bugfix: InSingletonScope bug when requesting an instance in OnActivation callback https://github.com/ninject/Ninject/issues/221 https://github.com/ninject/Ninject/issues/224 - Bugfix: The invoked member is not supported in a dynamic assembly https://github.com/ninject/Ninject/issues/225 +- Bugfix: Conditional binding is not being considered when score constructors https://github.com/ninject/Ninject/issues/237 Version 3.2 --------------- diff --git a/src/Ninject.Test/Integration/ConstructorSelectionTests.cs b/src/Ninject.Test/Integration/ConstructorSelectionTests.cs index fa56348..f3161ad 100644 --- a/src/Ninject.Test/Integration/ConstructorSelectionTests.cs +++ b/src/Ninject.Test/Integration/ConstructorSelectionTests.cs @@ -60,6 +60,19 @@ public void FirstAvailableWithBindingAvailableIsUsed() barracks.Weapon.Should().NotBeNull(); } + [Fact] + public void UnsatisfiedConditionalShouldBeIngored() + { + kernel.Bind().ToSelf(); + kernel.Bind().To(); + kernel.Bind().To().When(_ => false); + + var barracks = kernel.Get(); + barracks.Should().NotBeNull(); + barracks.Warrior.Should().BeNull(); + barracks.Weapon.Should().NotBeNull(); + } + [Fact] public void CtorWithMostDependenciesIsUsedWhenBindingsAreAvailable() { diff --git a/src/Ninject/Selection/Heuristics/StandardConstructorScorer.cs b/src/Ninject/Selection/Heuristics/StandardConstructorScorer.cs index 31548d8..62fd078 100644 --- a/src/Ninject/Selection/Heuristics/StandardConstructorScorer.cs +++ b/src/Ninject/Selection/Heuristics/StandardConstructorScorer.cs @@ -108,7 +108,9 @@ protected virtual bool BindingExists(IContext context, ITarget target) protected virtual bool BindingExists(IReadOnlyKernel kernel, IContext context, ITarget target) { var targetType = this.GetTargetType(target); - return kernel.GetBindings(targetType).Any(b => !b.IsImplicit) + var request = context.Request.CreateChild(targetType, context, target); + + return kernel.GetBindings(targetType).Any(b => !b.IsImplicit && b.Matches(request)) || target.HasDefaultValue; }