简体   繁体   English

在运行时绑定Ninject

[英]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 在startUp,我有以下绑定

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 我想自动更改内核中ApplicationSettings实例的值

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<> ? Rebind <>怎么样?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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