Ninject different behaviour between Kernel.Get and Constructor Injection

我只是一个虾纸丫 提交于 2019-12-05 18:49:09

With constructor injection, ninject translates the ctor parameter IBla[] to a IResolutionRoot.GetAll<IBla>().ToArray(). That's how support for multi-injection is implemented. So it's not possible for a ctor-request to result in a IResolutionRoot.Get<IBla[]>() - but it's still something you can do manually.

This is true for all collection-types which ninject translates to multi-injection (AFAIR array, IList, IEnumerable, but not ICollection).

I'd recommend using another collection interface (like ICollection) or collection implementation as constructor parameter. This will result in consistent behavior for ctor-injection and IResolutionRoot.Get calls.

It is possible to bind array dependencies in a specific order. You just need to register them in Ninject like this.

_kernel.Bind<Bla1>().ToSelf();
_kernel.Bind<Bla>().ToSelf();
_kernel.Bind<IConsumer>().To<Consumer>()
    .WithConstructorArgument("array", 
        new IBla[] { 
            _kernel.Get<Bla1>(), 
            _kernel.Get<Bla>() 
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!