Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 889
David MZ Avatar asked May 26 '12 18:05

David MZ


1 Answers

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.

like image 54
Daniel Marbach Avatar answered Sep 21 '22 06:09

Daniel Marbach