具有约束力ToConstant并呼吁InSingletonScope多余的? [英] Is binding ToConstant and calling InSingletonScope redundant?

查看:253
本文介绍了具有约束力ToConstant并呼吁InSingletonScope多余的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,这个问题是非常简单的标题说明



有关一个局部变量工厂

  VAR厂= Fluently.Configure()
...

这些是两行等效的:

 绑定< ISessionFactory> ()ToConstant(工厂).InSingletonScope()。 

 绑定< ISessionFactory方式>()ToConstant(工厂); 


解决方案

在ninject的最新版本,当你创建一个, ToConstant结合它会自动将范围设置为单例。因此, InSingletonScope()您的示例中的一部分是多余的。从ninject代码库:

  ///<总结> 
///表示服务应绑定到指定的恒定值。
///< /总结>
///< PARAM NAME =值>在恒定值< /参数>
公共IBindingWhenInNamedWithOrOnSyntax< T> ToConstant(T值)
{
Binding.ProviderCallback = CTX =>新ConstantProvider< T>(值);
Binding.Target = BindingTarget.Constant;
Binding.ScopeCallback = StandardScopeCallbacks.Singleton;

返回这一点;
}


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);

解决方案

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

这篇关于具有约束力ToConstant并呼吁InSingletonScope多余的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆