Skip to content

Commit

Permalink
Fixed #224
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-xu committed Sep 11, 2017
1 parent 3dcf70a commit 969b20b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Ninject.Test/Integration/ConstructorSelectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ public void WhenConstructorHasAnOpenGenericTypeItCountsAsServedParameterIfBindin
instance.Generic.Should().NotBeNull();
}

[Fact]
public void DoNotChooseObsoleteConstructors()
{
kernel.Bind<ClassWithObsoleteContructor>().ToSelf();

var instance = kernel.Get<ClassWithObsoleteContructor>();

instance.Sword.Should().NotBeNull();
}

#if !SILVERLIGHT
[Fact]
public void WhenConstructorHasAValueWithDefaultValueItCountsAsServedParameter()
Expand Down Expand Up @@ -286,5 +296,20 @@ public ClassWithTwoInjectAttributes(int someValue)
{
}
}

public class ClassWithObsoleteContructor
{
[Obsolete]
public ClassWithObsoleteContructor()
{
}

public ClassWithObsoleteContructor(Sword sword)
{
this.Sword = sword;
}

public Sword Sword { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,11 @@ public ConstructorInjectionDirective(ConstructorInfo constructor, ConstructorInj
/// </summary>
/// <value><c>true</c> if this constructor has an inject attribute; otherwise, <c>false</c>.</value>
public bool HasInjectAttribute { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this contructor has an obsolete attribute.
/// </summary>
/// <value><c>true</c> if this constructor has an obsolete attribute; otherwise, <c>false</c>.</value>
public bool HasObsoleteAttribute { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace Ninject.Planning.Strategies
{
using System;
using System.Collections.Generic;
using System.Reflection;
using Ninject.Components;
Expand Down Expand Up @@ -72,9 +73,11 @@ public void Execute(IPlan plan)
foreach (ConstructorInfo constructor in constructors)
{
var hasInjectAttribute = constructor.HasAttribute(this.Settings.InjectAttribute);
var hasObsoleteAttribute = constructor.HasAttribute(typeof(ObsoleteAttribute));
var directive = new ConstructorInjectionDirective(constructor, this.InjectorFactory.Create(constructor))
{
HasInjectAttribute = hasInjectAttribute,
HasObsoleteAttribute = hasObsoleteAttribute,
};

plan.Add(directive);
Expand Down
5 changes: 5 additions & 0 deletions src/Ninject/Selection/Heuristics/StandardConstructorScorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public virtual int Score(IContext context, ConstructorInjectionDirective directi
return int.MaxValue;
}

if (directive.HasObsoleteAttribute)
{
return int.MinValue;
}

var score = 1;
foreach (ITarget target in directive.Targets)
{
Expand Down

0 comments on commit 969b20b

Please sign in to comment.