Ninject: Resolve dependency by name only
Asked Answered
D

1

6

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.
Decimeter answered 14/6, 2011 at 13:19 Comment(0)
E
13

The only way is to bind them as object

kernel.Bind<object>().To<MyClass>().Named("A")
kernel.Get<object>("A");
Extortionary answered 14/6, 2011 at 15:19 Comment(1)
Thanks, this will work nicely in a convention based binding pattern I am working on.Decimeter

© 2022 - 2024 — McMap. All rights reserved.