Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not choose constructors with an "Obsolete" attribute #224

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Ninject.Test/Integration/ConstructorSelectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ public void WhenConstructorHasAnOpenGenericTypeItCountsAsServedParameterIfBindin
instance.Generic.Should().NotBeNull();
}

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

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

instance.Should().NotBeNull();
instance.SomeObject.Should().NotBeNull();
instance.ObsoleteConstructorCalled.Should().Be(false);
}

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

public class ClassWithObsoleteContructor
{
[Obsolete("Use Ninject to create an instance")]
public ClassWithObsoleteContructor()
: this(new object())
{
ObsoleteConstructorCalled = true;
}

public ClassWithObsoleteContructor(Object someObject)
{
SomeObject = someObject;
}

public Object SomeObject { get; set; }
public bool ObsoleteConstructorCalled { get; set; }
}
}
}
5 changes: 5 additions & 0 deletions src/Ninject/INinjectSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public interface INinjectSettings
/// </summary>
Type InjectAttribute { get; }

/// <summary>
/// Gets the attribute that indicates that a member is obsolete and should not be injected.
/// </summary>
Type ObsoleteAttribute { get; }

/// <summary>
/// Gets the interval at which the cache should be pruned.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Ninject/NinjectSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public Type InjectAttribute
set { this.Set("InjectAttribute", value); }
}

/// <summary>
/// Gets or sets the attribute that indicates that a member is obsolete and should not be injected.
/// </summary>
public Type ObsoleteAttribute
{
get { return this.Get("ObsoleteAttribute", typeof(ObsoleteAttribute)); }
set { this.Set("ObsoleteAttribute", value); }
}

/// <summary>
/// Gets or sets the interval at which the GC should be polled.
/// </summary>
Expand Down
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 constructor 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 @@ -72,9 +72,11 @@ public void Execute(IPlan plan)
foreach (ConstructorInfo constructor in constructors)
{
var hasInjectAttribute = constructor.HasAttribute(this.Settings.InjectAttribute);
var hasObsoleteAttribute = constructor.HasAttribute(this.Settings.ObsoleteAttribute);
var directive = new ConstructorInjectionDirective(constructor, this.InjectorFactory.Create(constructor))
{
HasInjectAttribute = hasInjectAttribute
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