When should parameterless ctor be used?

3 views
Skip to first unread message

Ryan Bergman

unread,
Nov 13, 2010, 5:02:03 PM11/13/10
to ninject
Greetings,
Accoding to the documentation on the github wiki ctor injection
should choose based on:

If a constructor has an [Inject] attribute, it is used.
If no constructors have an [Inject] attribute, Ninject will select the
one with the most parameters that Ninject understands how to resolve.
If no constructors are defined, Ninject will select the default
parameterless constructor.


However I found that this unit test fails. Is this a bug? I found a
similar thread from last spring but there was no resolution.

[Test]
public void NoParamCtorShouldBeUsed(){
var dojo = new StandardKernel();

Assert.That(dojo.Get< Foo>().NoParamCtorUsed, Is.True);
}

public class Foo
{
public bool NoParamCtorUsed;
public bool AmNothingWasUsed;

public Foo(IAmNothing nothing)
{
AmNothingWasUsed = true;
}

public Foo()
{
NoParamCtorUsed = true;
}
}


public interface IAmNothing {}


If I reverse the physical order of the ctors in the class then the
test passes.

Ian Davis

unread,
Nov 14, 2010, 11:36:28 AM11/14/10
to nin...@googlegroups.com
I have a fix for this, just reviewing.


--
You received this message because you are subscribed to the Google Groups "ninject" group.
To post to this group, send email to nin...@googlegroups.com.
To unsubscribe from this group, send email to ninject+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ninject?hl=en.




--
Ian Davis
http://innovatian.com
Reply all
Reply to author
Forward
0 new messages