Is there an OOB way in Ninject to configure service in singleton scope?

48 Views Asked by At

I am using ninject with ninject.extenions.conventions. I would like to use a convention to initialize some services as singletons. I know it is straightforward to add a SingletonAttribute and use it with .WithAttribute<SingletonAttribute>() during configuration. But I don't want to reinvent the wheel.

1

There are 1 best solutions below

0
On BEST ANSWER

There's no singleton attribute being deliver with Ninject or ninject conventions. The question is how you want to determine which types are bound .InSingletonScope(). Instead of an attribute you could also filter for types whose name end's with Service. Or which implement a specific interface. or which implement a specific attribute.

I would consider going for a name-based convention, if that does make sense. If not, you'll have to decide between shared interface or an attribute. If there's no shared methods/properties then a marker interface (empty interface) does not make sense and you should go with attributes.