Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject binding at runtime

I am looking at a certain scenario in an application that i am currently working on

I want an Admin officer to be able to change system wide settings in an application.

public class ApplicationSettings
{
 //bla bla bla

 }

At startUp, I have the following binding

public static void RegisterServices(IKernel kernel)
{
  kernel.Bind<ApplicationSettings>().ToSelf().InSingletonScope();
}

All is well and Good as I understand that the same instance of the application settings will be served for as long as the kernel is active

My question is this. What if I have to change the applicationsettings at runtime. And I want to automaticcally change the value of the ApplicationSettings instance in the kernel

Will it be possible to do something like this

public void ChangeSettings(IKernel kernel, ApplicationSettings setting)
{
   var setting = kernel.Get<ApplicationSettings>();
   //change the values of the instance
}

Question, How do i update the kernel binding so that subsequent references to the singleton instance will refer to the newly modified version

Thanks

like image 251
Seth IK Avatar asked Oct 25 '13 10:10

Seth IK


1 Answers

What about Rebind<> ?

public void ChangeSettings(IKernel kernel, ApplicationSettings setting)
{
   var setting = kernel.Get<ApplicationSettings>();
   kernel.Rebind<ApplicationSettings>().ToConstant(setting);
}
like image 182
Cybermaxs Avatar answered Oct 22 '22 17:10

Cybermaxs