1
votes

Is it possible to configure Ninject so that it automatically binds a type of interface to a concrete type based on the class naming conventions? I'm using Ninject version 3.

For example I have a lot of repositories in my domain layer named ITypeRepository which are implemented in my infrastructure layer as ProviderTypeRepository.

Below is an example of how I'm currently binding these is Ninject's CreateKernal method.

kernel.Bind<IClientRepository>().To<ProviderClientRepository>();
kernel.Bind<IVacancyRepository>().To<ProviderVacancyRepository>();
kernel.Bind<ICandidateRepository>().To<ProviderCandidateRepository>();
...etc etc

What I'd like is to somehow map this in one pass so that whenever I add a new repository I don't need to manually bind it.

1

1 Answers

1
votes

You're looking for the Ninject Conventions Extension which can do something like:

kernel.Bind(x => x
    .FromThisAssembly()
    .SelectAllClasses().EndingWith("MySuffix")
    .BindAllInterfaces();