David MZ
David MZ

Reputation: 3718

How do I tell ninject 3 to use singleton scope by default for all types?

I want my application to always use services as singletons, how do I set up Ninject to use singleton scope by default. I am using conventions to register my types, do I need to use the Bind<> method?

Upvotes: 5

Views: 1740

Answers (1)

Daniel Marbach
Daniel Marbach

Reputation: 2314

Hy, Assuming all your services inherit from IService you can write the following

Add the following using statement

using Ninject.Extensions.Conventions;

Use the conventions like

kernel.Bind( x => x
.FromThisAssembly()
.SelectAllClasses().InheritedFrom<IService>()
.BindAllInterfaces()
.Configure(b => b.InSingletonScope()));

You might need to tweek it a little bit to your needs.

Upvotes: 8

Related Questions