views:

75

answers:

1

Do I need to do something different in an abstract class to get dependency injection working with Ninject?

I have a base controller with the following code:

public abstract class BaseController : Controller
{
    public IAccountRepository AccountRepository
    {
        get;
        set;
    }
}

My module looks like this:

public class WebDependencyModule : NinjectModule
{
    public override void Load()
    {
        Bind<IAccountRepository>().To<AccountRepository>();
    }
}

And this is my Global.asax:

protected override void OnApplicationStarted()
{
    Kernel.Load(new WebDependencyModule());
}

protected override IKernel CreateKernel()
{
    return new StandardKernel();
}

It works when I decorate the IAccountRepository property with the [Inject] attribute.

+1  A: 

Not sure what you're trying to do.

It looks like you want to do Property Injection. If so, you have to stick on the attribute.

Ninject doesnt randomly go sticking things in properties.

Even if it could, you wonldnt want it to from the point of view of trying to understand what depends on what (I've weaned myself completely off PI).

If you want to do constructor injection, the concrete Controller will need to ask for one and pass it down to 'BaseController'.

Ninject will walk through to Object and inject Attributed properties, but doesnt treat abstract classes in any special manner.

Either that or I'm missing something.

Ruben Bartelink