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

时间:2011-09-12 23:07:14

标签: c# ninject

嗯,标题很简单地说明了这个问题。

对于局部变量factory

var factory = Fluently.Configure()
...

这两行是否相同:

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

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

1 个答案:

答案 0 :(得分:24)

在最新版本的ninject中,当您创建ToConstant绑定时,它会自动将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;
    }