Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is binding ToConstant and calling InSingletonScope redundant?

Tags:

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); 
like image 755
joshperry Avatar asked Sep 12 '11 23:09

joshperry


1 Answers

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;     } 
like image 80
Andrew Savinykh Avatar answered Oct 18 '22 11:10

Andrew Savinykh