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

喜欢而已 提交于 2019-12-12 04:36:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!