Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure Ninject to inject the NodaTime IClock

In my NinjectConfigurator I have

container.Bind<IClock>().To<SystemClock>(); 

I have also tried

container.Bind<IClock>().To<SystemClock>().InSingletonScope();

But I get this error:

Error activating IClock using binding from IClock to SystemClock No constructor was available to create an instance of the implementation type.

Activation path: 3) Injection of dependency IClock into parameter clock of constructor of type SystemManager 2) Injection of dependency ISystemManager into parameter systemManager of constructor of type AccountController 1) Request for AccountController

Suggestions: 1) Ensure that the implementation type has a public constructor. 2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.

This is my class with the injection, same as all other working classes with IoC in my project:

private readonly IDateTime _dateTime;
private readonly IClock _clock;

public SystemManager(IDateTime dateTime, IClock clock)
{
    this._dateTime = dateTime;
    this._clock = clock;
}

I couldn't find anything to help me on this. Any help is much appreciated.

like image 533
Brad Jeffery Avatar asked Nov 11 '15 06:11

Brad Jeffery


1 Answers

I haven't used NInject myself for a while, but I believe you want to use ToConstant() to bind to the instance of SystemClock:

container.Bind<IClock>().ToConstant(SystemClock.Instance);
like image 55
Jon Skeet Avatar answered Sep 19 '22 02:09

Jon Skeet