Ninject 给出 NullReferenceException

发布于 2024-08-27 14:51:25 字数 1190 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

围归者 2024-09-03 14:51:25

您是否考虑过使用构造函数注入?

这就是我使用 Ninject 2 & 进行依赖注入的方式。 ASP.NET MVC 2,它从控制器到整个链一直工作 ->服务->存储库和超过。

对我来说,在对象的构造函数中拥有依赖项也很有意义。它使这些依赖关系对于必须实例化它的任何其他对象都高度可见和明显。否则,您可能会遇到空引用异常...有点像您这里的情况。

HTH,
查尔斯

编辑:通过构造函数显示基类注入以响应评论。

public class BaseService
{
    public IRoundRepository RoundRepo { get; private set; }

    public BaseService(IRoundRepository roundRepo)
    {
        RoundRepo = roundRepo;
    }
}

public class SquareService : BaseService
{
    public ISquareRepository SquareRepo { get; private set; }

    public SquareService(ISquareRepository squareRepo, IRoundRepository roundRepo)
        : base(roundRepo)
    {
        SquareRepo = squareRepo;
    }
}

这只是我做事的方式......其他人可能有不同的想法/意见。

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.

public class BaseService
{
    public IRoundRepository RoundRepo { get; private set; }

    public BaseService(IRoundRepository roundRepo)
    {
        RoundRepo = roundRepo;
    }
}

public class SquareService : BaseService
{
    public ISquareRepository SquareRepo { get; private set; }

    public SquareService(ISquareRepository squareRepo, IRoundRepository roundRepo)
        : base(roundRepo)
    {
        SquareRepo = squareRepo;
    }
}

This is just my way of doing things... someone else may have a different idea / opinion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文