They are multiple connections of the ninject, guaranteeing the preservation of their binding

If I register:

Bind<IWeapon>().To<Sword>(); Bind<IWeapon>().To<Knife>(); Bind<IWeapon>().To<ChuckNorris>(); 

And then extract through:

 IEnumerable<IWeapon> weapons = ServiceLocator.Current.GetAllInstances<IWeapon>(); 

I'm sure bindings will always be returned in that order?

I tried this, and it seems to be so, but it may be purely random.

+7
source share
2 answers

Short answer: No, you are not!

Longer answer: the current implementation keeps order. But this does not guarantee that this will still occur in future versions of Ninject. Also, you should not have such business rules in your IoC container configuration.

+6
source

I found a way to make ordered multi-bindings, because I need this too:

(the truth is very similar to my answer here: fooobar.com/questions/364487 / ... )

  // Binding public sealed class FooModule: NinjectModule { public opverride void Load() { Bind<IReadOnlyList<IFoo>>().ToMethod(c=>new IFoo[] { c.Kernel.Get<FooType1>(), c.Kernel.Get<FooType2>(), ... }); } } // Injection target public class InjectedClass { public InjectedClass(IReadOnlyList<IFoo> foos) { ;} } 

I agree that just indicating that future versions will maintain the declaration order is the best solution, but this workaround works.

I would like to create a child object c in the context of c, so Get would know that it is being implemented in InjectedClass, but I could not figure out how to do this.

0
source

Source: https://habr.com/ru/post/899773/


All Articles