Ninject binding at runtime
Asked Answered
R

1

7

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

Rival answered 25/10, 2013 at 10:5 Comment(0)
H
11

What about Rebind<> ?

public void ChangeSettings(IKernel kernel, ApplicationSettings setting)
{
   var setting = kernel.Get<ApplicationSettings>();
   kernel.Rebind<ApplicationSettings>().ToConstant(setting);
}
Handel answered 25/10, 2013 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.