Ninject: Registering an already created instance with Ninject?

You can use the ToConstant method which takes an already existing instance and registers it as singleton.

var kernel = new StandardKernel();
kernel.Bind<MyClass>().ToConstant(myClassInstance);

If you want to something more complex you can use the ToMethod (where you can use a Func to get your instance) combined with the InSingletonScope

var kernel = new StandardKernel();
kernel.Bind<MyClass>().ToMethod(context => myClassInstance).InSingletonScope();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)