Is binding ToConstant and calling InSingletonScope redundant?

二次信任 提交于 2019-11-28 02:29:14

问题


Well, this question is pretty simply stated by the title.

For a local variable factory:

var factory = Fluently.Configure()
...

Are these two lines equivalent:

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

and:

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

回答1:


In the latest version of ninject, when you create a ToConstant binding it will automatically set the Scope to Singleton. Thus, the InSingletonScope() part in your example is redundant. From ninject code base:

    /// <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;
    }


来源:https://stackoverflow.com/questions/7395071/is-binding-toconstant-and-calling-insingletonscope-redundant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!