Ninject giving NullReferenceException

被刻印的时光 ゝ 提交于 2019-12-10 11:27:43

问题


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( ).To( ).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( )  );
}

回答1:


Sorry, I can't answer why that isn't working the way it should but 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.



来源:https://stackoverflow.com/questions/2574706/ninject-giving-nullreferenceexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!