This project is read-only.

Question about Ninject logic to choose which constructor to call

Jul 11, 2013 at 3:27 PM
Hi everyone,
I was reading Ninject documentation and I found following information:

1.If the type only has a single constructor, Ninject will call it.
2.If the type has more than one constructor, but has a default (parameterless) constructor available, Ninject will call it. (This also applies to types that have no explicit constructors defined.)

For testing reason I created following classes and interfaces:

using Ninject;
using ConsoleApplication3;
class Samurai : IWarrior
{
private readonly IWeapon _weapon;
//My default (parameterless) constructor was not created
public Samurai()
{

}
//But this constructor was created , why?
public Samurai(IWeapon weapon)
{
    _weapon = weapon;
}

public void Attack(string target)
{
    _weapon.Hit(target);
}
}
interface IWeapon
{
    void Hit(string target);
}    
interface IWarrior
{
    void Attack(string target);
}
class Hummer : IWeapon
{
    public Hummer()
    {
    }

    public void Hit(string target)
    {
        Console.WriteLine("{0} smashed with hummer", target);
    }
}
class Program
{
    static void Main(string[] args)
    {
        var kernel = new StandardKernel(new WarrorModule());
        var warrior = kernel.Get<IWarrior>();
        warrior.Attack("bee");
        Console.Read();
    }
}

class WarrorModule : NinjectModule
{
    public override void Load()
    {
        Bind<IWarrior>().To<Samurai>();
        Bind<IWeapon>().To<Hummer>();
    }
}
In my example public Samurai(IWeapon weapon) was creaded instead of public Samurai().
I tried to google algorithm about case when code don't have any "[Inject]" attributes, but I found just https://ninject.codeplex.com/wikipage?title=Injection%20Patterns this information
Looks like documentation is wrong. Am I right?