使用 NInject 连接依赖关系的选项
使用 NInject(最好是 2.0),我们有哪些选项可以在 Web 应用程序中连接我们的对象依赖项?
它们可以在 XML 配置文件中定义吗?
还是必须通过代码来完成?
With NInject (preferably 2.0), what options do we have wrt wiring up our object dependencies in a web application?
Can they be defined in an XML configuration file?
Or does it have to be done via code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个基于 xml 的配置扩展:https://github.com/ninject/ninject.extensions。 不过,
您可以在代码中进行更强大的绑定。
There is an extension for xml based configuration: https://github.com/ninject/ninject.extensions.xml
You can do a lot more powerful binding in code though.
Ninject 没有 XML 配置,抱歉,但我无法提供直接链接(因为他们的网站有 flash 元素),但这里引用的是 ninject.org:
Ninject doesn't have XML configuration, sorry but I can't provide a direct link (cos their site has flash elements), but here is a quotation from ninject.org:
我发现仅在代码中定义绑定的问题是您必须添加对 dll 的引用。
如果不添加对新 dll 的引用(删除对旧 dll 的引用)、更改代码并重新编译,则无法更改绑定。
如果我们有 xml 配置,我根本不需要引用,也不需要重新编译。
现在我有 MVC 应用程序,它使用 DI 将存储库传递给控制器。用于添加绑定的 Ninject 代码仅使用存储库的具体实现。我仍然需要添加对包含实现的 dll 的引用。只需一行代码!
或者也许有可能使用 Ninject 来实现这一点?
The problem I see with defining bindings in the code only is that you have to add reference to the dll.
You cannot change the binding without adding reference to new dll (removing reference to old one), change code and recompile.
If we had xml config I wouldn't need reference at all, and wouldn't have to recompile.
Right now I have MVC app that is using DI to pass repositories to Controllers. Nothing else then Ninject code for adding bindings uses the concrete implementations of repositories. And still I need to add reference to dll containing the implementations. For only one line of code!
Or maybe there is a possibility to achieve this using Ninject?
您希望实现什么目标?您想要配置什么类型的东西?动态选择策略 ?传递端口号?您可以提供更多关于您的想法的信息,以获得更好的答案[您可以接受:P]。
您需要分割以下方面的关注点:
appSettings
插件/未知对象接线(MEF?)在第一个池中,在代码中执行此操作是正确的方法,我想不出任何优势XML 会给出,特别是。在强名称等的上下文中
What are you looking to achieve? What sort of stuff are you looking to configure? Dynamically selecting a Strategy ? Passing in Port numbers? You could offer a lot more information as to what you're thinking in order to get a better answer [that you can acccept :P].
You need to split the concerns of:
appSettings
In the first pool, doing it in Code is just the right way and I cant think of any advantage XML would give, esp. in the context of strong names etc.