Ninject中是否有OOB方式在单例范围内配置服务? [英] Is there an OOB way in Ninject to configure service in singleton scope?

查看:275
本文介绍了Ninject中是否有OOB方式在单例范围内配置服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将ninjectninject.extenions.conventions一起使用.我想使用一个约定将某些服务初始化为单例.我知道在配置期间添加SingletonAttribute并将其与.WithAttribute<SingletonAttribute>()一起使用很简单.但是我不想重新发明轮子.

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.

推荐答案

Ninject或ninject约定没有传递单例属性. 问题是如何确定.InSingletonScope()绑定了哪些类型.除了属性,您还可以过滤名称以Service结尾的类型.或者哪个实现一个特定的接口.或哪个实现了特定属性.

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.

这篇关于Ninject中是否有OOB方式在单例范围内配置服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆