如何在Ninject中更改现有绑定的范围 [英] How to change the scope of an existing binding in Ninject

查看:67
本文介绍了如何在Ninject中更改现有绑定的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个模块中,我为对象设置了绑定.还有其他两个模块:测试模块和Web模块. Web模块希望该绑定处于请求范围内,而测试模块希望该绑定处于单例范围内.现在,我们只是复制整个绑定并添加适当的范围.有一个更好的方法吗?我正在寻找一种方法,可以在一个模块中进行绑定本身(这是一个ToMethod绑定),然后仅使测试和Web模块更改该绑定的作用域.

In one module, I have a binding set up for an object. There are two other modules: a testing module and a web module. The web module wants that binding to be in request scope, and the testing module wants that binding to be in singleton scope. Right now, we are just duplicating the entire binding and adding the appropriate scope. Is there a better way to do this? I am looking for a way that I can make the binding itself (it's a ToMethod binding) in the one module, and then just have the testing and web modules just change the scope on that binding.

推荐答案

使用Ninject,只需使用Bind<T>().ToSelf()-然后添加所需的任何作用域(例如,Bind<T>().ToSelf().InRequestScope()).

Using Ninject, simply use the Bind<T>().ToSelf() - and then add whatever scope you want (Bind<T>().ToSelf().InRequestScope(), for example).

这篇关于如何在Ninject中更改现有绑定的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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