使用 NInject 连接依赖关系的选项

发布于 2024-08-19 05:01:33 字数 107 浏览 10 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晨与橙与城 2024-08-26 05:01:33

有一个基于 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.

离笑几人歌 2024-08-26 05:01:33

Ninject 没有 XML 配置,抱歉,但我无法提供直接链接(因为他们的网站有 flash 元素),但这里引用的是 ninject.org

摆脱 XML 的束缚

大多数其他 .NET 依赖注入
框架是围绕使用而设计的
XML 来声明类型绑定。
而不是强迫你写
繁琐且容易出错的文本,
Ninject 为您提供流畅的
接口,它可以让您连接
您的应用程序的各个部分使用
完整的代码。这意味着您可以
利用的特点
IDE 和编译器,如代码完成
和类型安全。

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:

Free yourself from XML

Most other .NET dependency injection
frameworks are designed around the use
of XML to declare type bindings.
Rather than forcing you to write
cumbersome and error-prone text,
Ninject arms you with a fluent
interface, which lets you connect the
pieces of your application using
full-fledged code. This means you can
take advantage of the features of the
IDE and compiler, like code completion
and type-safety.

一腔孤↑勇 2024-08-26 05:01:33

我发现仅在代码中定义绑定的问题是您必须添加对 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?

避讳 2024-08-26 05:01:33

您希望实现什么目标?您想要配置什么类型的东西?动态选择策略 ?传递端口号?您可以提供更多关于您的想法的信息,以获得更好的答案[您可以接受:P]。

您需要分割以下方面的关注点:

  1. 已知对象连接 (DI)
  2. 配置 - 通常您会希望将它们分割成小的集中子集,例如强类型配置元素与在大型配置中拥有全局设置池 混在一起堆
  3. 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:

  1. known object wiring (DI)
  2. configuration - generally you'll want to split those into small focused subsets e.g. Strongly Typed config elements vs having a global pool of settings in a big pile mishmashed together a la appSettings
  3. plugins / unknown object wiring (MEF?)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文