Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind all concrete implementations by name in Ninject

I have an interface IAdapdor, and several concrete implementations. Using Ninject 3.0, I bind them all by name:

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?

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
like image 446
bavaza Avatar asked Nov 23 '25 03:11

bavaza


1 Answers

You can customize the convention created bindings using the Configure method. So you can use that to register your bindings with Named:

kernel.Bind(x => x
    .FromThisAssembly()
    .SelectAllClasses().InheritedFrom<IAdapdor>()
    .BindAllInterfaces()
    .Configure((b, c) => b.Named(c.Name)));
like image 198
nemesv Avatar answered Nov 24 '25 19:11

nemesv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!