Ninject : Resolving an object by type _and_ registration name/identifier
-
22-08-2019 - |
Question
I am looking for a way to do something like this with Ninject :
// Sample from the Unity application block
IMyService result = myContainer.Resolve<IMyService>("Data");
( from http://msdn.microsoft.com/en-us/library/cc440957.aspx )
Is it possible?
Solution
Ninject 2.0 has this capability:
Bind<IMyService>().To<MyServiceA>().Named("Data");
Bind<IMyService>().To<MyServiceB>().Named("SomethingElse");
kernel.Get<IMyService>("Data"); // will return MyServiceA
OTHER TIPS
AFAIK there is no way to do that directly in Ninject, but you can use Contextual Binding instead.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow