Search code examples
ninjectmef

What is the equivalent of MEFs CreationPolicy.NonShared in Ninject


I know that CreationPolicy.Shared means singleton, as explained in here in this SO question.

So what about NonShared?

Should I have something like this?

Bind<IDataRepositoryFactory>().To<DataRepositoryFactory>().InTransientScope();

Or should I leave it without any scope like

Bind<IDataRepositoryFactory>().To<DataRepositoryFactory>();

What is the difference between the above two?


Solution

  • The default scope in Ninject is Transient, which effectively means a new instance will be created every time one is requested, so there is no difference between your two examples.

    More on Ninject scopes here:

    • Transient - A new instance of the type will be created each time one is requested. This is the default scope if none is specified.
    • Singleton - Only a single instance of the type will be created, and the same instance will be returned for each subsequent request.
    • Thread - One instance of the type will be created per thread.
    • Request - One instance of the type will be created for each Web Request. See the Ninject.Web.Common InRequestScope article for more information before using this.
    • Named, Call, Parent - Supplied by an extension. See Ninject.Extensions.NamedScope extension for more information before using this.
    • Custom - You can also easily define your own scopes using the .InScope(Func selectScope) method.