Search code examples
c#ninjectninject-2

Ninject: Resolve dependency by name only


I have a WPF view\view-model binding pattern where I would like to resolve dependencies from Ninject by name only, rather than by type or type+name. I want to bind my view-models by name with Ninject, and then refer to the view-models in views by this name for view injection (via Caliburn.Micro).

I realize that in practice multiple types could be registered against the same name, but I want a convention type pattern and am willing to live with this case. I only need to resolve to "object" for WPF binding to work.

For instance, is there some way I can:

  • Retrieve all bindings regardless of what types they are registered against.
  • Probe for a binding with the appropriate name.
  • Create an instance via the binding.

Solution

  • The only way is to bind them as object

    kernel.Bind<object>().To<MyClass>().Named("A")
    kernel.Get<object>("A");