将依赖项注入控制器构造函数时出错
我正在 ASP.NET MVC 中启动一个项目,使用 Ninject 来实现依赖注入。这是我当前的架构:
抽象存储库
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);
}
具体存储库
public class UserRepository : IAccountRepository<User>
{
// implementation omitted
}
public class ComicBookIssuesRepository
: IRepository<ComicBookIssue>
{
// implementation omitted
}
控制器
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;
}
}
依赖注入
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>();
}
}
IRepository
到 ComicBookIssuesRepository
工作正常,因为它能够构造并注入 ComicBookIssueController
错误。但是,当尝试注入 AccountController
时,它会抛出以下错误:
激活 IAccountRepository{User} 时出错 没有匹配的绑定 可用,并且类型不可自绑定。激活路径:
2) 将依赖项 IAccountRepository{User} 注入参数 AccountController 类型构造函数的存储库
1) 请求 帐户控制器建议:
1) 确保您已定义绑定 IAccountRepository{用户}。
2) 如果绑定是在模块中定义的, 确保该模块已加载到内核中。
3)确保 您并没有意外地创建了多个内核。
4)如果你是 使用构造函数参数,确保参数名称匹配 构造函数参数名称。
5) 如果您使用自动模块 正在加载,请确保搜索路径和过滤器正确。
我无法弄清楚是什么导致了一个依赖项的错误,而不是另一个依赖项的错误。两者遵循相同的模式,只是实现不同的接口。有其他人遇到过此错误或者可以发现我的实现存在问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我原来的帖子的评论中,Remo 关于冲突代码的说法是正确的。我的项目中存在
User
模型类的冲突版本,但我忘记将其删除。控制器引用此类,而 Ninject 绑定到其他版本。删除旧版本解决了该问题。In the comments on my original post, Remo was correct about conflicting code. There was a conflicting version of the
User
model 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.