Question

Ninject looks great, so I'd like to use it in my project. Unfortunately I am still struggling to do the most trivial binding. The [Inject] attribute compiles just fine, but the compiler cannot find the "Bind" command:

using System;
using Ninject.Core;
using Ninject.Core.Binding;

namespace NinjectTest
{
    public interface IFoo
    {
        void DoSomething();
    }

    public class Foo : IFoo
    {
        public void DoSomething()
        {
            throw new NotImplementedException();
        }
    }

    public class Bar
    {
        [Inject] private IFoo theFoo;

        public Bar()
        {
            Bind<IFoo>().To<Foo>(); //Compiler Error: "The name 'Bind' does not exist in the current context"
        }
    }
}

What could be going wrong here?

Was it helpful?

Solution

The Bind method is a method in the Ninject StandardModule class. You need to inherit that class to be able to bind.

Here is a simple example:

using System; 
using System.Collections.Generic; 
using System.Text; 
using Ninject.Core;

namespace Forecast.Domain.Implementation 
{
    public class NinjectBaseModule : StandardModule
    {
        public override void Load()
        {
            Bind<ICountStocks>().To<Holding>();
            Bind<IOwn>().To<Portfolio>();
            Bind<ICountMoney>().To<Wallet>();
        }
    } 
}

OTHER TIPS

The Bind method is defined in ModuleBase - you should inherit your class from this or, even better, from StandardModule.

I don't know the Ninject, but on first look I see, that the "Bind" method is not member of "Bar" class or their base class. Propably you need some instance with "Bind" method or static class with static "Bind" method.

After quick googling, I think the "Bind" method is part of instance members of "InlineMethod" class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top