1

I have a set of repositories in a class lib that are required to be singletons. They share library with other classes as well - classes created as singletons.

Is it possible to configure Ninject to automatically use InSingletonScope() for all classes that are matching a specific criteria - for example, defined in a specific namespace?

I have between 20 and 30 repositories and from what I have found, I need to call InSingletonScope() for every repository.

I can - of course - do this by reflection, but I'm interested to know if there is an "automatic approach" to this :)

ForestC
  • 213
  • 1
  • 12

2 Answers2

3

From the Ninject.Extensions.Conventions wiki:

kernel.Bind(x => x
    .FromThisAssembly()
    .SelectAllClasses().InNamespaceOf<MyService>()
    .BindAllInterfaces()
    .Configure(b => b.InSingletonScope()));
Adam Rodger
  • 3,472
  • 4
  • 33
  • 45
1

This is dealt with by the ninject.extensions.conventions extension.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249