问题
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'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.
来源:https://stackoverflow.com/questions/26900262/is-there-an-oob-way-in-ninject-to-configure-service-in-singleton-scope