Stefan
Stefan

Reputation: 31

Using Generic Types while binding with Ninject - is it possible?

Using Ninject, in my main program I call:

var kernel = new StandardKernel(new MyBindings());
var stuff = kernel.Get<MediaPresenter>();

Unfortunately I get an exception:

No matching bindings are available, and the type is not self-bindable.

I really don't understand what that means. Here is my binding class:

class MyBindings : NinjectModule
{
    public override void Load()
    {
        Bind<MediaPresenter>().ToSelf();
        Bind(typeof (Dao<Book>)).To(typeof (Dao<Book>));
    }
}

If I remove the line:

Bind(typeof(Dao<Book>)).To(typeof(Dao<Book>));

The application runs, but then I get no bindings.

Why does that kind of thing not work and how can I fix it?

Upvotes: 3

Views: 1582

Answers (3)

BrokenGlass
BrokenGlass

Reputation: 160922

Can't test it right now but this should work:

Bind(typeof (Dao<>)).To(typeof(Dao<>));

Using an interface, a probably better idea going forward:

Bind(typeof (IDao<>)).To(typeof(Dao<>));

Upvotes: 2

Bobby
Bobby

Reputation: 1604

You should be codding against interface as it will make your code alot more testable. Have a look at this more info on Ninject binding

Ninject Binding

ok in your case you need to do this (not tested). As long as you tell Inject the concrete implmentations it will take care of the injection at the right places for you depending that you have the right inject attributes in place in your code.

  class MyBindings : NinjectModule
        {
            public override void Load()
            {
                Bind<IMediaPresenter>().To<MediaPresenter>;
                Bind<IDao>().To<Dao>();
                Bind<IBook>().To<Book>();
            }
        }

Upvotes: 0

tahir
tahir

Reputation: 1026

why don't do this in load :

Bind<Dao<Book>>.ToSelf();

Upvotes: 0

Related Questions