How do I setup NInject? (I'm getting can't resolve "Bind", in the line "Bind<IWeapon>().To<Sword>()

Posted by Greg on Stack Overflow See other posts from Stack Overflow or by Greg
Published on 2010-03-26T03:58:14Z Indexed on 2010/03/26 4:13 UTC
Read the original article Hit count: 387

Filed under:

Hi,

I'm getting confused in the doco how I should be setting up Ninject. I'm seeing different ways of doing it, some v2 versus v1 confusion probably included...

Question - What is the best way in my WinForms application to set things up for NInject (i.e. what are the few lines of code required). I'm assuming this would go into the MainForm Load method. In other words what code do I have to have prior to getting to:

Bind<IWeapon>().To<Sword>();

I have the following code, so effectively I just want to get clarification on the setup and bind code that would be required in my MainForm.Load() to end up with a concrete Samurai instance?

internal interface IWeapon
{
    void Hit(string target);
}

class Sword : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Chopped {0} clean in half", target);
    }
}

class Samurai
{
    private IWeapon _weapon;

    [Inject]
    public Samurai(IWeapon weapon)
    {
        _weapon = weapon;
    }

    public void Attack(string target)
    {
        _weapon.Hit(target);
    }
}

thanks

PS. I've tried the following code, however I can't resolve the "Bind". Where does this come from? what DLL or "using" statement would I be missing?

private void MainForm_Load(object sender, EventArgs e)
{
    Bind<IWeapon>().To<Sword>();   // <==  *** CAN NOT RESOLVE Bind ***
    IKernel kernel = new StandardKernel();
    var samurai = kernel.Get<Samurai>();

© Stack Overflow or respective owner

Related posts about ninject