3

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
bavaza
  • 10,319
  • 10
  • 64
  • 103
  • I am curious to hear what this convention is... because I don't see one. The Conventions extension doesn't solve everything... sometimes you need to explicitly bind things. – Simon Whitehead Jan 27 '14 at 09:30
  • @Simon Whitehead - from what I understand, I should be able to do this in a single line with conventions. I'll edit the question to reflect this. – bavaza Jan 27 '14 at 09:33

1 Answers1

1

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)));
nemesv
  • 138,284
  • 16
  • 416
  • 359