问题
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