Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 520
Tim Lloyd Avatar asked Jun 14 '11 13:06

Tim Lloyd


1 Answers

The only way is to bind them as object

kernel.Bind<object>().To<MyClass>().Named("A")
kernel.Get<object>("A");
like image 58
Remo Gloor Avatar answered Oct 12 '22 16:10

Remo Gloor