Skip to content

Commit 2eb191a

Browse files
committed
Merge branch 'master' of https://github.com/ninject/ninject
2 parents fc443b3 + 44ae50e commit 2eb191a

File tree

6 files changed

+56
-14
lines changed

6 files changed

+56
-14
lines changed

src/Ninject/Activation/Providers/StandardProvider.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,32 @@ public virtual object Create(IContext context)
7777
context.Plan = this.Planner.GetPlan(this.GetImplementationType(context.Request.Service));
7878
}
7979

80-
if (!context.Plan.Has<ConstructorInjectionDirective>())
80+
ConstructorInjectionDirective directive = null;
81+
var directives = context.Plan.ConstructorInjectionDirectives;
82+
83+
if (directives.Count == 1)
8184
{
82-
throw new ActivationException(ExceptionFormatter.NoConstructorsAvailable(context));
85+
directive = directives[0];
8386
}
84-
85-
var directives = context.Plan.GetAll<ConstructorInjectionDirective>();
86-
var bestDirectives = directives
87-
.GroupBy(option => this.ConstructorScorer.Score(context, option))
88-
.OrderByDescending(g => g.Key)
89-
.First();
90-
if (bestDirectives.Skip(1).Any())
87+
else
9188
{
92-
throw new ActivationException(ExceptionFormatter.ConstructorsAmbiguous(context, bestDirectives));
89+
var bestDirectives = directives
90+
.GroupBy(option => this.ConstructorScorer.Score(context, option))
91+
.OrderByDescending(g => g.Key)
92+
.FirstOrDefault();
93+
if (bestDirectives == null)
94+
{
95+
throw new ActivationException(ExceptionFormatter.NoConstructorsAvailable(context));
96+
}
97+
98+
if (bestDirectives.Skip(1).Any())
99+
{
100+
throw new ActivationException(ExceptionFormatter.ConstructorsAmbiguous(context, bestDirectives));
101+
}
102+
103+
directive = bestDirectives.First();
93104
}
94105

95-
var directive = bestDirectives.Single();
96106
var arguments = directive.Targets.Select(target => this.GetValue(context, target)).ToArray();
97107
return directive.Injector(arguments);
98108
}

src/Ninject/Planning/Directives/ConstructorInjectionDirective.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public class ConstructorInjectionDirective : MethodInjectionDirectiveBase<Constr
2626
/// </summary>
2727
public ConstructorInfo Constructor { get; set; }
2828

29+
/// <summary>
30+
/// Gets or sets a value indicating whether this constructor has an inject attribute.
31+
/// </summary>
32+
/// <value><c>true</c> if this constructor has an inject attribute; otherwise, <c>false</c>.</value>
33+
public bool HasInjectAttribute { get; set; }
34+
2935
/// <summary>
3036
/// Initializes a new instance of the <see cref="ConstructorInjectionDirective"/> class.
3137
/// </summary>

src/Ninject/Planning/IPlan.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public interface IPlan
2525
/// </summary>
2626
Type Type { get; }
2727

28+
/// <summary>
29+
/// Gets the constructor injection directives.
30+
/// </summary>
31+
/// <value>The constructor injection directives.</value>
32+
IList<ConstructorInjectionDirective> ConstructorInjectionDirectives { get; }
33+
2834
/// <summary>
2935
/// Adds the specified directive to the plan.
3036
/// </summary>

src/Ninject/Planning/Plan.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public class Plan : IPlan
3232
/// </summary>
3333
public ICollection<IDirective> Directives { get; private set; }
3434

35+
/// <summary>
36+
/// Gets the constructor injection directives.
37+
/// </summary>
38+
/// <value>The constructor injection directives.</value>
39+
public IList<ConstructorInjectionDirective> ConstructorInjectionDirectives { get; private set; }
40+
3541
/// <summary>
3642
/// Initializes a new instance of the <see cref="Plan"/> class.
3743
/// </summary>
@@ -42,6 +48,7 @@ public Plan(Type type)
4248

4349
Type = type;
4450
Directives = new List<IDirective>();
51+
ConstructorInjectionDirectives = new List<ConstructorInjectionDirective>();
4552
}
4653

4754
/// <summary>
@@ -51,6 +58,13 @@ public Plan(Type type)
5158
public void Add(IDirective directive)
5259
{
5360
Ensure.ArgumentNotNull(directive, "directive");
61+
62+
var constructorInjectionDirective = directive as ConstructorInjectionDirective;
63+
if (constructorInjectionDirective != null)
64+
{
65+
ConstructorInjectionDirectives.Add(constructorInjectionDirective);
66+
}
67+
5468
Directives.Add(directive);
5569
}
5670

@@ -62,7 +76,7 @@ public void Add(IDirective directive)
6276
public bool Has<TDirective>()
6377
where TDirective : IDirective
6478
{
65-
return GetAll<TDirective>().Count() > 0;
79+
return GetAll<TDirective>().Any();
6680
}
6781

6882
/// <summary>

src/Ninject/Planning/Strategies/ConstructorReflectionStrategy.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Reflection;
1414
using Ninject.Components;
1515
using Ninject.Infrastructure;
16+
using Ninject.Infrastructure.Language;
1617
using Ninject.Injection;
1718
using Ninject.Planning.Directives;
1819
using Ninject.Selection;
@@ -64,7 +65,12 @@ public void Execute(IPlan plan)
6465

6566
foreach(ConstructorInfo constructor in constructors)
6667
{
67-
plan.Add(new ConstructorInjectionDirective(constructor, InjectorFactory.Create(constructor)));
68+
var hasInjectAttribute = constructor.HasAttribute(Settings.InjectAttribute);
69+
plan.Add(
70+
new ConstructorInjectionDirective(constructor, InjectorFactory.Create(constructor))
71+
{
72+
HasInjectAttribute = hasInjectAttribute
73+
});
6874
}
6975
}
7076
}

src/Ninject/Selection/Heuristics/StandardConstructorScorer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public virtual int Score(IContext context, ConstructorInjectionDirective directi
5252
Ensure.ArgumentNotNull(context, "context");
5353
Ensure.ArgumentNotNull(directive, "constructor");
5454

55-
if (directive.Constructor.HasAttribute(Settings.InjectAttribute))
55+
if (directive.HasInjectAttribute)
5656
{
5757
return int.MaxValue;
5858
}

0 commit comments

Comments
 (0)