好吧,这个问题很简单地用标题来说明.
对于局部变量factory:
var factory = Fluently.Configure()
...
Run Code Online (Sandbox Code Playgroud)
这两行是否相同:
Bind<ISessionFactory>().ToConstant(factory).InSingletonScope();
Run Code Online (Sandbox Code Playgroud)
和:
Bind<ISessionFactory>().ToConstant(factory);
Run Code Online (Sandbox Code Playgroud)
And*_*ykh 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;
}
Run Code Online (Sandbox Code Playgroud)