I am in the midst of diving into unit testing/dependency injection/mocking. Using Ninject, I can bind an interface to an implementation as follows in my NinjectWebCommon.cs
:
kernel.Bind<IRecipeRepository>().To<RecipeRepository>();
and this works fine. However, I don't want to have to individually bind every interface to a concrete implementation. To overcome this, I have used standard naming conventions for interfaces (IFoo
is the interface of class Foo
) and am trying to use the following to give a default binding to all interfaces using Ninject.Extensions.Conventions
. Note: This code lives in the CreateKernel()
method in NinjectWebCommon.cs
:
kernel.Bind(c => c
.FromThisAssembly()
.IncludingNonePublicTypes()
.SelectAllClasses()
.BindDefaultInterface()
.Configure(y => y.InRequestScope()));
However, when I do this, I am receiving the following error:
Error activating IRecipeRepository
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency IRecipeRepository into parameter recipeRepository of constructor of type RecipesController
1) Request for RecipesController
All help is appreciated.
Edit: My controller's constructor looks as follows:
private IRecipeRepository recipeRepository;
private ISizeRepository sizeRepository;
[Inject]
public RecipesController(IRecipeRepository recipeRepository, ISizeRepository sizeRepository)
{
this.recipeRepository = recipeRepository;
this.sizeRepository = sizeRepository;
}