1
votes

I've got an MVC 4 application that I want to use Ninject with. I've used NuGet for the Ninject 3 packages, however I'm having some difficulties with the configuration. The NuGet install created the file:

public static class NinjectWebCommon 
{
     // Not included for brevity

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {

    }
}

I have a class I'm using to strongly type my web config. I've called this class AppEnvironment, and it has an interface of IAppEnvironment for unit testing. I know that within the RegisterSevices function I can do:

kernel.Bind<IAppEnvironment>().To<AppEnvironment>();

However this means that I have to specify each binding one at a time. In a previous project we had Ninject configured to bind by default to a new instance of the interface with just the I of the interface class removed. I've taken a look at Using NInject to bind a generic interface, with a default if a binding for the generic type is not set however I'm not sure what the IService in .InheritedFrom(typeof(IService<>)) is for, and what I should be using for my configuration.

Any clues on how to get this working?

1

1 Answers

2
votes

InheritedFrom(typeof(IService<>)) filters the types returned by SelectAllClasses() (all the types in the assembly) to include only the types that are IService<Something>.

In you scenario you might use the convention extensions and do:

kernel.Bind(x => x.FromThisAssembly()
                  .SelectAllClasses()
                  .BindDefaultInterface());

BindDefaultInterface assumes the naming convention that you described (which is common): e.g. Foo is bound to IFoo.