asp.net-mvc Ninject 与 Unity 的 DI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1054801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:41:09  来源:igfitidea点击:

Ninject vs Unity for DI

asp.net-mvcninjectunity-container

提问by Miral

We are using ASP.net MVC.

我们正在使用 ASP.net MVC。

Which of these is the best DI framework Ninject or Unity and why?

哪一个是最好的 DI 框架 Ninject 或 Unity,为什么?

采纳答案by Mendelt

Last time I looked at either of them I found Ninject slightly better. But both have their drawbacks.

上次我看它们中的任何一个时,我发现 Ninject 稍微好一些。但两者都有其缺点。

Ninject has a better fluent-configuration scheme. Unity seems to rely mostly on XML configuration. Ninject's main drawback is that it requires you to reference Ninject.Core everywhere in your code to add [Inject] attributes.

Ninject 有更好的流畅配置方案。Unity 似乎主要依赖于 XML 配置。Ninject 的主要缺点是它需要您在代码中随处引用 Ninject.Core 以添加 [Inject] 属性。

If I may ask, why are you limiting your choices to these two? I think Castle.Windsor, Autofac and StructureMap are at least as good or better.

如果我要问,为什么你的选择仅限于这两个?我认为 Castle.Windsor、Autofac 和 StructureMap 至少一样好或更好。

回答by roberocity

I know this is an old question, but here are my thoughts:

我知道这是一个老问题,但这是我的想法:

I personally like Ninject. I like the fluent interfaces and avoiding of XML. I generally like XML, just not for this kind of config stuff. Especially when refactoring is involved the fluent interfaces make it easier to correct.

我个人喜欢 Ninject。我喜欢流畅的界面和避免使用 XML。我一般喜欢 XML,只是不喜欢这种配置的东西。特别是当涉及重构时,流畅的接口更容易纠正。

I miss StructureMap's ObjectFactory, but there are easy workarounds to add that to Ninject.

我想念 StructureMap 的 ObjectFactory,但有一些简单的解决方法可以将其添加到 Ninject。

As Jeffery points out you don't have to use the [Inject] attribute when you only have one constructor.

正如 Jeffery 指出的那样,当您只有一个构造函数时,您不必使用 [Inject] 属性。

I found that I prefer the fluent interfaces not only because they avoid XML, but because they cause compile time errors when I change something that affected them. The XML configuration doesn't and the less I have to rememberto change the better off I am.

我发现我更喜欢 fluent 接口,不仅因为它们避免了 XML,还因为当我更改影响它们的某些内容时它们会导致编译时错误。XML 配置没有,我需要记住的更改越少,我的状态就越好。

回答by IMLiviu

Ninject detects circular dependencies if you use Injection Constructors as opposed to Unity that regardless of the injection technique just throws a StackOverflowException which is extremely hard to debug.

如果您使用注入构造函数而不是 Unity,则 Ninject 会检测循环依赖,无论注入技术如何,都只会抛出一个极难调试的 StackOverflowException。

回答by Johan Leino

I agree with Mendelt, there is no "best" DI framework. It just depends on the situation and they all have pros and cons. think David Hayden said on DotNet Rocks that Unity is the preferred choice if you use the rest of EntLib and are familiar with that. I personally use Unity because my customer likes the fact that it says MicrosoftEnterprise Library (Unity) on the DLLs, if you get what I′m saying.

我同意 Mendelt 的观点,没有“最好的”DI 框架。这只是取决于情况,它们都有优点和缺点。想想 David Hayden 在 DotNet Rocks 上说的,如果你使用 EntLib 的其余部分并且熟悉它,Unity 是首选。我个人使用 Unity,因为我的客户喜欢它在 DLL 上显示MicrosoftEnterprise Library (Unity)的事实,如果你明白我的意思的话。

I use both both xml configuration for setting up the interfaces and their concrete implementations but then I use attributes in code when injecting, like:

我同时使用 xml 配置来设置接口及其具体实现,但随后在注入时使用代码中的属性,例如:

<type type="ILogger" mapTo="EntLibLogger">
   <lifetime type="singleton"/>
</type>

and in code:

并在代码中:

[InjectionConstructor]
public Repository([Dependency] ILogger logger)

Personally I think that makes it clearer what happens, but of course one could argue that you will have references to unity all over your application. It′s up to you.

就我个人而言,我认为这可以更清楚地说明发生了什么,但当然有人可能会争辩说,您将在整个应用程序中引用统一性。由你决定。