绑定ToConstant并调用InSingletonScope是多余的?

好吧,这个问题很简单地用标题来说明。

对于本地变量factory

 var factory = Fluently.Configure() ... 

这两行是否相同:

 Bind().ToConstant(factory).InSingletonScope(); 

和:

 Bind().ToConstant(factory); 

在最新版本的ninject中,当您创建ToConstant绑定时,它会自动将Scope设置为Singleton。 因此,示例中的InSingletonScope()部分是多余的。 来自ninject代码库:

  ///  /// Indicates that the service should be bound to the specified constant value. ///  /// The constant value. public IBindingWhenInNamedWithOrOnSyntax ToConstant(T value) { Binding.ProviderCallback = ctx => new ConstantProvider(value); Binding.Target = BindingTarget.Constant; Binding.ScopeCallback = StandardScopeCallbacks.Singleton; return this; }