简体   繁体   中英

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

What about Rebind<> ?

public void ChangeSettings(IKernel kernel, ApplicationSettings setting)
{
   var setting = kernel.Get<ApplicationSettings>();
   kernel.Rebind<ApplicationSettings>().ToConstant(setting);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM