gpt4 book ai didi

c# - 绑定(bind) ToConstant 和调用 InSingletonScope 是多余的吗?

转载 作者:IT王子 更新时间:2023-10-29 04:31:04 25 4
gpt4 key购买 nike

好吧,这个问题从标题就很简单了。

对于局部变量factory:

var factory = Fluently.Configure()
...

这两行是否等价:

Bind<ISessionFactory>().ToConstant(factory).InSingletonScope();

和:

Bind<ISessionFactory>().ToConstant(factory);

最佳答案

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

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

return this;
}

关于c# - 绑定(bind) ToConstant 和调用 InSingletonScope 是多余的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7395071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com