From b47ffc001b15e965127327ce32cc0fa3665f4426 Mon Sep 17 00:00:00 2001 From: Remo Gloor Date: Sat, 11 Jun 2011 03:19:53 +0200 Subject: [PATCH] Fix StandardConstructorScorer so that it does not fails unit test in some situations --- .../Heuristics/StandardConstructorScorer.cs | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Ninject/Selection/Heuristics/StandardConstructorScorer.cs b/src/Ninject/Selection/Heuristics/StandardConstructorScorer.cs index 96470597..b170e032 100644 --- a/src/Ninject/Selection/Heuristics/StandardConstructorScorer.cs +++ b/src/Ninject/Selection/Heuristics/StandardConstructorScorer.cs @@ -48,13 +48,10 @@ public int Score(IContext context, ConstructorInjectionDirective directive) int score = 1; foreach(ITarget target in directive.Targets) { - foreach(IParameter parameter in context.Parameters) + if (ParameterExists(context, target)) { - if(string.Equals(target.Name, parameter.Name)) - { - score++; - continue; - } + score++; + continue; } Type targetType = target.Type; @@ -63,12 +60,25 @@ public int Score(IContext context, ConstructorInjectionDirective directive) if(targetType.IsGenericType && targetType.GetInterfaces().Any(type => type == typeof(IEnumerable))) targetType = targetType.GetGenericArguments()[0]; - - if(context.Kernel.GetBindings(targetType).Any()) + + if (context.Kernel.GetBindings(targetType).Any()) + score++; + else + { score++; + if (score > 0) + { + score += Int32.MinValue; + } + } } return score; } + + private static bool ParameterExists(IContext context, ITarget target) + { + return context.Parameters.Any(parameter => string.Equals(target.Name, parameter.Name)); + } } }