简体   繁体   English

在Ninject中按名称绑定所有具体实现

[英]Bind all concrete implementations by name in Ninject

I have an interface IAdapdor , and several concrete implementations. 我有一个接口IAdapdor ,以及几个具体的实现。 Using Ninject 3.0, I bind them all by name: 使用Ninject 3.0,我按名称绑定它们:

IKernel kernel = new StandardKernel();

kernel.Bind<IAdapdor>().To<Adaptor1>().Named("Adaptor1");
kernel.Bind<IAdapdor>().To<Adaptor2>().Named("Adaptor2");
...

How can I achieve this using Ninject conventions extension ? 如何使用Ninject约定扩展实现此目的?

To be more specific, I'm looking for something in the line: 更具体地说,我正在寻找一些东西:

kernel.Bind(x => x.FromThisAssembly()
                  .SelectAllClasses()
                  .InheritedFrom<IAdapdor>()
                  .BindByClassName()); // <-- BindByClassName() does not really exist

You can customize the convention created bindings using the Configure method . 您可以使用Configure方法自定义约定创建的绑定。 So you can use that to register your bindings with Named : 所以你可以使用它来Named你的绑定Named

kernel.Bind(x => x
    .FromThisAssembly()
    .SelectAllClasses().InheritedFrom<IAdapdor>()
    .BindAllInterfaces()
    .Configure((b, c) => b.Named(c.Name)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM