I am starting a project in ASP.NET MVC using Ninject to implement dependency injection. Here is my current architecture:
Abstract Repositories
public interface IAccountRepository<T> where T : class
{
IQueryable<T> Retrieve();
T RetrieveByID(int id);
T RetrieveByEmail(string name);
bool Create(T entity);
bool Update(T entity);
bool Delete(T entity);
}
public interface IRepository<T> where T : class
{
IQueryable<T> Retrieve();
T RetrieveByID(int id);
T RetrieveByName(string name);
bool Create(T entity);
bool Update(T entity);
bool Delete(T entity);
}
Concrete Repositories
public class UserRepository : IAccountRepository<User>
{
// implementation omitted
}
public class ComicBookIssuesRepository
: IRepository<ComicBookIssue>
{
// implementation omitted
}
Controllers
public class AccountController : Controller
{
private IAccountRepository<User> userRepository;
public AccountController(
IAccountRepository<User> repository)
{
this.userRepository = repository;
}
}
public class ComicBookIssueController : Controller
{
private IRepository<ComicBookIssue> issueRepository;
public ComicBookIssueController(
IRepository<ComicBookIssue> repository)
{
this.issueRepository = repository;
}
}
Dependency Injection
public class NinjectControllerFactory
: DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(
RequestContext requestContext, Type controllerType)
{
return controllerType == null ? null :
(IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
ninjectKernel.Bind<IRepository<ComicBookIssue>>()
.To<ComicBookIssuesRepository>();
ninjectKernel.Bind<IAccountRepository<User>>()
.To<UserRepository>();
}
}
The binding of IRepository<ComicBookIssue> to ComicBookIssuesRepository works fine, as it is able to construct and inject the ComicBookIssueController with no errors. However, when trying to inject into the AccountController, it throws the following error:
Error activating IAccountRepository{User} No matching bindings are
available, and the type is not self-bindable. Activation path:
2)
Injection of dependency IAccountRepository{User} into parameter
repository of constructor of type AccountController
1) Request for
AccountControllerSuggestions:
1) Ensure that you have defined a binding for
IAccountRepository{User}.
2) If the binding was defined in a module,
ensure that the module has been loaded into the kernel.
3) Ensure
you have not accidentally created more than one kernel.
4) If you are
using constructor arguments, ensure that the parameter name matches
the constructors parameter name.
5) If you are using automatic module
loading, ensure the search path and filters are correct.
I can’t figure out what is causing the error for one dependency and not the other. The two follow the same pattern, just implement different interfaces. Has any one else experienced this error or can find a problem with my implementation?
In the comments on my original post, Remo was correct about conflicting code. There was a conflicting version of the
Usermodel class left in my project that I forgot to remove. The controller was referencing this class, while Ninject was binding to the other version. Removing the old version solved the issue.