Ninject 给出 NullReferenceException
我正在使用 asp.net MVC 2 和 Ninject 2。
设置非常简单。 控制器调用调用存储库的服务。
在我的控制器中,我使用注入来实例化服务类,没有任何问题。但服务类不会实例化存储库,从而给我 NullReferenceException。
public class BaseController : Controller
{
[Inject]
public IRoundService roundService { get; set; }
}
这有效。 这不会...
public class BaseService
{
[Inject]
public IRoundRepository roundRepository { get; set; }
}
但是,当我尝试在 RoundService 类中使用 roundRepository 时,
IList<Round> rounds = roundRepository.GetRounds( );
给出 NullReferenceException 。模块类 -
public class ServiceModule : NinjectModule
{
public override void Load( )
{
Bind<IRoundService>( ).To<RoundService>( ).InRequestScope( );
}
}
public class RepositoryModule : NinjectModule
{
public override void Load( )
{
Bind<IRoundRepository>( ).To<RoundRepository>( ).InRequestScope( );
}
}
在 global.axax.cs 中
protected override IKernel CreateKernel( )
{
return new StandardKernel( new ServiceModule( ),
new RepositoryModule( ) );
}
I'm using asp.net MVC 2 and Ninject 2.
The setup is very simple.
Controller calls service that calls repository.
In my controller I use inject to instantiate the service classes with no problem. But the service classes don't instantiate the repositories, giving me NullReferenceException.
public class BaseController : Controller
{
[Inject]
public IRoundService roundService { get; set; }
}
This works. But then this does not...
public class BaseService
{
[Inject]
public IRoundRepository roundRepository { get; set; }
}
Giving a NullReferenceException, when I try to use the roundRepository in my RoundService class.
IList<Round> rounds = roundRepository.GetRounds( );
Module classes -
public class ServiceModule : NinjectModule
{
public override void Load( )
{
Bind<IRoundService>( ).To<RoundService>( ).InRequestScope( );
}
}
public class RepositoryModule : NinjectModule
{
public override void Load( )
{
Bind<IRoundRepository>( ).To<RoundRepository>( ).InRequestScope( );
}
}
In global.axax.cs
protected override IKernel CreateKernel( )
{
return new StandardKernel( new ServiceModule( ),
new RepositoryModule( ) );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过使用构造函数注入?
这就是我使用 Ninject 2 & 进行依赖注入的方式。 ASP.NET MVC 2,它从控制器到整个链一直工作 ->服务->存储库和超过。
对我来说,在对象的构造函数中拥有依赖项也很有意义。它使这些依赖关系对于必须实例化它的任何其他对象都高度可见和明显。否则,您可能会遇到空引用异常...有点像您这里的情况。
HTH,
查尔斯
编辑:通过构造函数显示基类注入以响应评论。
这只是我做事的方式......其他人可能有不同的想法/意见。
Have you thought about using constructor injection?
That's how I do my dependency injection with Ninject 2 & ASP.NET MVC 2 and it works all the way down the chain from controller -> service -> repository & beyond.
It also makes sense to me to have the dependencies in the constructor for your object. It makes these dependencies highly visible and obvious to any other object that has to instantiate it. Otherwise you may end up with null reference exceptions... kinda like you have here.
HTHs,
Charles
EDIT: Showing base class injection through constructors in response to the comments.
This is just my way of doing things... someone else may have a different idea / opinion.