Adrian Grigore
Adrian Grigore

Reputation: 33318

Compilation Error with Ninject

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?

Upvotes: 0

Views: 606

Answers (3)

David M
David M

Reputation: 72930

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

Upvotes: 3

Halvard
Halvard

Reputation: 4001

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>();
        }
    } 
}

Upvotes: 5

TcKs
TcKs

Reputation: 26642

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.

Upvotes: 0

Related Questions