在 ASP.NET 核心 MVC 中继续 Ninject 支持?

Continued Ninject support in ASP.NET Core MVC?

我一直很高兴地使用Ninject很长一段时间,我真的很喜欢它,但是自从ASP.NET Core和[=14=发布以来我面临着一个艰难的选择].

基本上,微软一开始就透露了他们自己的依赖注入系统;据我所知,这是一个受到了很多批评的人。但我更大的问题在于它如何影响其他图书馆。

and other resources online 开始,Ninject 似乎无法与 MVC Core 开箱即用。尽管有一个以详细库形式给出的“解决方案”Microsoft.Framework.DependencyInjection.Ninject and Ninject。这甚至更棘手,因为该库需要将 https://www.myget.org/F/aspnetmaster/ 添加到您的 NuGet 提要列表中。

我进行了一些挖掘,找到了这个库的托管位置;看起来不错,据我所知似乎工作正常,但有几件事让我很困扰。

所以基本上,我非常担心这是某种创可贴,对 Ninject(甚至其他容器库)的支持正在消失。是否有一些我没有发现的隐藏信息?

  • The library does not really appear to be headed by the Ninject creators

那个图书馆,似乎 these also, look to be Microsoft created samples of Dependency Injection providers that were removed in beta7. Note that the link to DI in MVC 6 referenced by your 说了以下内容;

These DI container adaptors are temporary and are there for reference; we expect that they will eventually be removed and replaced by the respective container owners.

理应如此。 Microsoft 不应负责维护第 3 方提供程序。

  • The library is buried pretty deep in an obscure repository

如果您不知道,ASP.NET 5 仍在开发中。 Beta 7 在 nuget 上作为预发布版提供,但还有其他来源,包括;

这些资源由 Microsoft 维护。

  • The actual Ninject resources online never mention it

与任何新开发一样,第 3 方库提供商必须自己确定何时(如果有的话)他们将提供支持新代码库的产品实现。对于一些人来说,等到新框架正式发布被认为是最有效的,因为 API 重大变化仍然很可能在那之前发生。是否会完全实施支持当然取决于提供者的资源,and/or 在开源社区的情况下。

现有 DI 库的维护者正在讨论是否为新的 ASP.NET 内置 DI 系统构建、维护和支持适配器。 Autofac 维护者 confirmed that they will create and support an adapter, while the Ninject team has been silent, and other teams such as the Simple Injector team (that includes me) have explained 表示他们不支持适配器。

个人认为 ASP.NET Core 内置的 DI 库是一个不错的干净的 DI 库,但它仅限于简单的应用程序。正如我所解释的 here,不支持开发围绕 SOLID 原则构建的可维护应用程序所需的许多功能。然而,就像几年前 Unity DI 库所做的那样,我认为这个内置容器实际上可能会触发开发人员开始使用依赖注入,这对我们行业来说是一个胜利。

这些限制使得内置容器特别适合配置和扩展 ASP.NET 系统本身。要构建大型可维护应用程序,您将需要使用不同的 DI 库。这当然很好;您必须为这项工作选择合适的工具。

不幸的是,到目前为止,ASP.NET 团队已经沟通过 publicly that using a different DI library, means you will have to write/use an adapter. This unfortunately is the wrong message IMO, because most DI libraries are incompatible with the API presented by the built-in container (as I explained here and here in detail). Only Autofac seems reasonably in sync, which explains why the Autofac team choose to maintain an adapter. But do note that even Autofac has proven to be incompatible with the abstraction that Microsoft defined, and they (just like StructureMap) had to make big changes to their product to even be able to comply with the abstraction. And the Autofac maintainers are severely frustrated about the whole process and the abstraction in general. And as I explained here,甚至 ASP.NET 提供的 Ninject 适配器实现也被破坏了。

ASP.NET 团队使用适配器的消息在我看来是一个大错误,因为这会扼杀创新(而 DI 库本身不会;它只是另一个 DI 库)。 ASP.NET 团队正在推广一种模型,在该模型中,您的应用程序组件和 ASP.NET 系统(以及未来将插入的所有其他子系统)都将在您的自定义容器中注册。将您的应用程序配置与 ASP.NET 系统的配置分开更加合理和实用(如 here 所述)。

因此,我发现对任何容器使用适配器都毫无用处。正如我展示的 here 插入您自己的 DI 容器真的很容易,同时保持它与 ASP.NET 的注册完全分开。这意味着您不需要 Ninject 的支持就可以在 ASP.NET 核心项目上有效地使用 Ninject。 Ninject 唯一需要做的就是创建一个与 .NET Core 兼容的版本(以防您的产品需要在该新平台上 运行)。

更新:"Ninject 3.3.0 was released September 26th 2017 and now targets .NET Standard 2.0 and thus also runs on .NET Core 2.0."

所以简而言之,我不确定支持 'is dying out',尽管一些 DI 维护者(例如 Simple Injector 团队,可能还有 Castle Windsor 和 Ninject)选择了不为 ASP.NET Core 构建、维护和支持适配器实现,因为它不需要,只是碍事。

2016 年 11 月更新

我一直在discussing some improvements to ASP.NET Core with Microsoft to make it easier to plugin a container that don't have an adapter (take a look at my example repository and especially to the Startup.cs of the Ninject sample project), but until now Microsoft seems to stall progress because (as Fowler states hisself) their "bias towards conforming containers[是]遮蔽[他们]的视线。