C# 哪些 .NET 依赖注入框架值得研究?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21288/
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-08-01 09:07:54  来源:igfitidea点击:

Which .NET Dependency Injection frameworks are worth looking into?

提问by pbreault

Which C#/.NET Dependency Injection frameworks are worth looking into? And what can you say about their complexity and speed.

哪些 C#/.NET 依赖注入框架值得研究?您对它们的复杂性和速度有什么看法。

采纳答案by RobertTheGrey

edit(not by the author): There is a comprehensive list of IoC frameworks available at https://github.com/quozd/awesome-dotnet/blob/master/README.md#ioc:

编辑(不是作者):https: //github.com/quozd/awesome-dotnet/blob/master/README.md#ioc 上有一个完整的 IoC 框架列表:

  • Castle Windsor- Castle Windsor is best of breed, mature Inversion of Control container available for .NET and Silverlight
  • Unity- Lightweight extensible dependency injection container with support for constructor, property, and method call injection
  • Autofac- An addictive .NET IoC container
  • DryIoc- Simple, fast all fully featured IoC container.
  • Ninject- The ninja of .NET dependency injectors
  • StructureMap- The original IoC/DI Container for .Net
  • Spring.Net- Spring.NET is an open source application framework that makes building enterprise .NET applications easier
  • LightInject- A ultra lightweight IoC container
  • Simple Injector- Simple Injector is an easy-to-use Dependency Injection (DI) library for .NET 4+ that supports Silverlight 4+, Windows Phone 8, Windows 8 including Universal apps and Mono.
  • Microsoft.Extensions.DependencyInjection- The default IoC container for ASP.NET Core applications.
  • Scrutor- Assembly scanning extensions for Microsoft.Extensions.DependencyInjection.
  • VS MEF- Managed Extensibility Framework (MEF) implementation used by Visual Studio.
  • TinyIoC- An easy to use, hassle free, Inversion of Control Container for small projects, libraries and beginners alike.
  • Castle Windsor- Castle Windsor 是同类最佳、成熟的控制反转容器,可用于 .NET 和 Silverlight
  • Unity- 轻量级的可扩展依赖注入容器,支持构造函数、属性和方法调用注入
  • Autofac- 一个令人上瘾的 .NET IoC 容器
  • DryIoc- 简单、快速、功能齐全的 IoC 容器。
  • Ninject- .NET 依赖注入器的忍者
  • StructureMap- .Net 的原始 IoC/DI 容器
  • Spring.Net- Spring.NET 是一个开源应用程序框架,可以更轻松地构建企业 .NET 应用程序
  • LightInject- 超轻量级 IoC 容器
  • Simple Injector- Simple Injector 是一个易于使用的依赖注入 (DI) 库,适用于 .NET 4+,支持 Silverlight 4+、Windows Phone 8、Windows 8,包括通用应用程序和 Mono。
  • Microsoft.Extensions.DependencyInjection- ASP.NET Core 应用程序的默认 IoC 容器。
  • Scrutor- Microsoft.Extensions.DependencyInjection 的程序集扫描扩展。
  • VS MEF- Visual Studio 使用的托管可扩展性框架 (MEF) 实现。
  • TinyIoC- 适用于小型项目、图书馆和初学者的易于使用、无障碍的控制反转容器。

Original answer follows.

原答案如下。



I suppose I might be being a bit picky here but it's important to note that DI (Dependency Injection) is a programming pattern and is facilitated by, but does not require, an IoC (Inversion of Control) framework. IoC frameworks just make DI much easier and they provide a host of other benefits over and above DI.

我想我在这里可能有点挑剔,但重要的是要注意 DI(依赖注入)是一种编程模式,由 IoC(控制反转)框架促进,但不需要。IoC 框架只是让 DI 变得更容易,并且它们提供了许多超越 DI 的其他好处。

That being said, I'm sure that's what you were asking. About IoC Frameworks; I used to use Spring.Netand CastleWindsora lot, but the real pain in the behind was all that pesky XML config you had to write! They're pretty much all moving this way now, so I have been using StructureMapfor the last year or so, and since it has moved to a fluent config using strongly typed generics and a registry, my pain barrier in using IoC has dropped to below zero! I get an absolute kick out of knowing now that my IoC config is checked at compile-time (for the most part) and I have had nothing but joy with StructureMap and its speed. I won't say that the others were slow at runtime, but they were more difficult for me to setup and frustration often won the day.

话虽如此,我相信这就是你要问的。关于 IoC 框架;我曾经经常使用Spring.NetCastleWindsor,但背后真正的痛苦是您必须编写的所有讨厌的 XML 配置!他们现在几乎都在朝着这个方向发展,所以我在过去一年左右的时间里一直在使用StructureMap,自从它使用强类型泛型和注册表转向流畅的配置后,我在使用 IoC 时的痛苦障碍已经下降到零下!现在我知道我的 IoC 配置是在编译时检查的(大部分情况下),我感到非常兴奋,而且我对 StructureMap 及其速度感到非常满意。我不会说其他的在运行时很慢,但它们对我来说更难设置,而且挫折常常赢得了这一天。

Update

更新

I've been using Ninjecton my latest project and it has been an absolute pleasure to use. Words fail me a bit here, but (as we say in the UK) this framework is 'the Dogs'. I would highly recommend it for any green fields projects where you want to be up and running quickly. I got all I needed from a fantastic set of Ninject screencastsby Justin Etheredge. I can't see that retro-fitting Ninject into existing code being a problem at all, but then the same could be said of StructureMapin my experience. It'll be a tough choice going forward between those two, but I'd rather have competition than stagnation and there's a decent amount of healthy competition out there.

我一直在我的最新项目中使用Ninject,使用起来非常愉快。词在这里有点让我失望,但是(正如我们在英国所说)这个框架是“狗”。我强烈推荐它用于您想要快速启动和运行的任何绿色领域项目。我从Justin Etheredge 的一组精彩的 Ninject 截屏视频中得到了我需要的一切。我完全看不出将 Ninject 改装到现有代码中是一个问题,但根据我的经验,StructureMap也是如此。在这两者之间前进将是一个艰难的选择,但我宁愿有竞争而不是停滞不前,而且那里有相当多的良性竞争。

Other IoC screencasts can also be found here on Dimecasts.

其他 IoC 截屏视频也可以在 Dimecasts 上找到

回答by ScottKoon

Ninject is great. It seems really fast, but I haven't done any comparisons. I know Nate, the author, did some comparisons between Ninject and other DI frameworks and is looking for more ways to improve the speed of Ninject.

Ninject 很棒。看起来真的很快,但我没有做过任何比较。我知道作者 Nate 对 Ninject 和其他 DI 框架做了一些比较,并且正在寻找更多方法来提高 Ninject 的速度。

I've heard lots of people I respect say good things about StructureMap and CastleWindsor. Those, in my mind, are the big three to look at right now.

我听到很多我尊敬的人都说 StructureMap 和 CastleWindsor 的好话。在我看来,这些是目前需要关注的三大巨头。

回答by Jason Sparks

I've used Spring.NETin the past and had great success with it. I never noticed any substantial overhead with it, though the project we used it on was fairly heavy on its own. It only took a little time reading through the documentationto get it set up.

我过去使用过Spring.NET并取得了巨大的成功。我从未注意到它有任何实质性的开销,尽管我们使用它的项目本身就相当沉重。只需花一点时间阅读文档即可进行设置。

回答by Ian Patrick Hughes

The great thing about C# is that it is following a path beaten by years of Java developers before it. So, my advice, generally speaking when looking for tools of this nature, is to look for the solid Java answer and see if there exists a .NET adaptation yet.

C# 的伟大之处在于,它走上了一条被多年 Java 开发人员打败的道路。所以,我的建议,一般来说,在寻找这种性质的工具时,是寻找可靠的 Java 答案,看看是否存在 .NET 改编版本。

So when it comes to DI (and there are so many options out there, this really is a matter of taste) is Spring.NET. Additionally, it's always wise to research the people behind projects. I have no issue suggesting SourceGear products for source control (outside of using them) because I have respect for Eric Sink. I have seen Mark Pollack speak and what can I say, the guy just gets it.

所以说到 DI(而且有很多选择,这真的是一个品味问题)是Spring.NET。此外,研究项目背后的人总是明智的。我没有问题建议 SourceGear 产品进行源代码控制(除了使用它们),因为我尊重 Eric Sink。我见过马克波拉克说话,我能说什么,这家伙明白了。

In the end, there are a lot of DI frameworks and your best bet is to do some sample projects with a few of them and make an educated choice.

最后,有很多 DI 框架,最好的办法是用其中的几个做一些示例项目,然后做出明智的选择。

Good luck!

祝你好运!

回答by Pete

Autofac. https://github.com/autofac/AutofacIt is really fast and pretty good. Here is a link with comparisons (made after Ninject fixed a memory leak issue).

自动法。 https://github.com/autofac/Autofac真的很快,也很不错。这是一个带有比较的链接(在 Ninject 修复了内存泄漏问题之后制作)。

http://www.codinginstinct.com/2008/05/ioc-container-benchmark-rerevisted.html

http://www.codinginstinct.com/2008/05/ioc-container-benchmark-rerevisted.html

回答by Rob Bazinet

I think a good place to start is with Ninject, it is new and has taken into account alot of fine tuning and is really fast. Nate, the developer, really has a great site and great support.

我认为 Ninject 是一个很好的起点,它是新的,并且考虑了很多微调,而且速度非常快。开发人员 Nate 确实拥有很棒的站点和强大的支持。

回答by Julio César

I can recommend Ninject. It's incredibly fast and easy to use but only if you don't need XML configuration, else you should use Windsor.

我可以推荐 Ninject。它非常快速且易于使用,但前提是您不需要 XML 配置,否则您应该使用 Windsor。

回答by Chris Canal

I'm a huge fan of Castle. I love the facilities it also provides beyond the IoC Container story. It really simplfies using NHibernate, logging, AOP, etc. I also use Binsorfor configuration with Boo and have really fallen in love with Boo as a language because of it.

我是城堡的忠实粉丝。我喜欢它在 IoC 容器故事之外还提供的设施。使用 NHibernate、日志记录、AOP 等真的很简单。我也使用Binsor来配置 Boo,因此我真的爱上了 Boo 作为一种语言。

回答by David Robbins

Spring.Net is quite solid, but the documentation took some time to wade through. Autofac is good, and while .Net 2.0 is supported, you need VS 2008 to compile it, or else use the command line to build your app.

Spring.Net 非常可靠,但文档需要一些时间来完成。Autofac 很好,虽然支持 .Net 2.0,但你需要 VS 2008 来编译它,或者使用命令行来构建你的应用程序。

回答by Glenn Block

It depends on what you are looking for, as they each have their pros and cons.

这取决于您要寻找什么,因为它们各有优缺点。

  1. Spring.NETis the most mature as it comes out of Spring from the Java world. Spring has a very rich set of framework libraries that extend it to support Web, Windows, etc.
  2. Castle Windsoris one of the most widely used in the .NET platform and has the largest ecosystem, is highly configurable / extensible, has custom lifetime management, AOP support, has inherent NHibernate support and is an all around awesome container. Windsor is part of an entire stack which includes Monorail, Active Record, etc. NHibernate itself builds on top of Windsor.
  3. Structure Maphas very rich and fine grained configuration through an internal DSL.
  4. Autofacis an IoC container of the new age with all of it's inherent functional programming support. It also takes a different approach on managing lifetime than the others. Autofac is still very new, but it pushes the bar on what is possible with IoC.
  5. NinjectI have heard is more bare bones with a less is more approach (heard not experienced).
  6. The biggest discriminator of Unityis: it's from and supported by Microsoft (p&p). Unity has very good performance, and great documentation. It is also highly configurable. It doesn't have all the bells and whistles of say Castle / Structure Map.
  1. Spring.NET是最成熟的,因为它来自 Java 世界的 Spring。Spring 有一组非常丰富的框架库,可以扩展它以支持 Web、Windows 等。
  2. Castle Windsor是 .NET 平台中使用最广泛的平台之一,拥有最大的生态系统,高度可配置/可扩展,具有自定义生命周期管理,AOP 支持,具有固有的 NHibernate 支持,并且是一个全方位的出色容器。Windsor 是整个堆栈的一部分,包括 Monorail、Active Record 等。NHibernate 本身构建在 Windsor 之上。
  3. Structure Map通过内部 DSL 具有非常丰富和细粒度的配置。
  4. Autofac是新时代的 IoC 容器,具有所有固有的函数式编程支持。它还采用与其他方法不同的方法来管理生命周期。Autofac 仍然很新,但它推动了 IoC 的可能性。
  5. Ninject我听说用少即是多的方法更简单(听说没有经验)。
  6. 最大的区别Unity是:它来自并由 Microsoft (p&p) 提供支持。Unity 具有非常好的性能和出色的文档。它也是高度可配置的。它没有城堡/结构图的所有花里胡哨。

So in summary, it really depends on what is important to you. I would agree with others on going and evaluating and seeing which one fits. The nice thing is you have a nice selection of donuts rather than just having to have a jelly one.

所以总而言之,这真的取决于什么对你来说很重要。我会同意其他人去评估和查看哪一个适合。好消息是你有很多甜甜圈,而不仅仅是一个果冻。