Ninject giving NullReferenceException
Asked Answered
M

1

6

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( )  );
}
Mallet answered 4/4, 2010 at 14:28 Comment(1)
12 years on I'm still getting upvotes on this, is this really still an issue?Mallet
P
1

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.

Pigeonwing answered 5/4, 2010 at 5:20 Comment(3)
I'm downvoting this because it does not answer the question, as it says in the first line.Canberra
@DanielLobo - I removed the offending textPigeonwing
Thanks for the attention, but the problem is not the text it self, it is the fact that it not answers the question. Even with the way you are doing things, I still have the problem because Ninject fails catastrophically in same scenarios without even saying why. (There is na index out of bound exception that Ninject throws in some cases).Canberra

© 2022 - 2024 — McMap. All rights reserved.