Prism.Forms 哪个 IoC 容器更好 [英] Which IoC container is better with Prism.Forms

查看:27
本文介绍了Prism.Forms 哪个 IoC 容器更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始一个新的 Prism.Forms 项目,我想知道各种 IoC containers (AutofacDrylocNinjectUnity) 最好继续使用.

I am in the start of a new Prism.Forms project and I was wondering which of the various IoC containers (Autofac, Dryloc, Ninject or Unity) would be best to move forward with.

我不知道这是否属实,但我在某处读到 Unity 不再处于积极开发状态,因为这和 MEF 是唯一的 IoC 容器我用过我不确定它是否适合.

I do not know if this is true, but I read somewhere that Unity is no longer under active development and since this and MEF are the only IoC containers I have ever used I am unsure as to whether it is the way to go.

与此同时,我对 AutofacDrylocNinject 知之甚少或一无所知.

Meanwhile, I know little or nothing about Autofac, Dryloc or Ninject.

请在任何建议中保持客观,提供您认为一个比其他更好的原因,而不是简单地我使用 xxx";我想做出明智的决定.

Please be objective in any advise, providing reasons why you feel one is better than the others rather than simply "I use xxx"; I would like to make an informed decision.

推荐答案

我能做的最好的事情就是按照目前的情况排列事实.

The best I can do is to layout the facts as they currently stand.

注意:自 2017 年我第一次回答这个问题以来发生了很多变化.下面的信息一直在更新,应该可以帮助您就项目做出决策.但是请注意,您现在还可以使用 Prism.Container.Extensions,因为它支持抽象许多非常强大的注册方法,包括工厂,以及将单个实现注册为不同服务的单例的能力.可以在那里添加额外的容器.可以在 https://prismplugins.com

NOTE: A lot has changed since I first answered this in 2017. The information below has continued to be updated and should help you as you look to make decisions on your projects. Do take note however that you do also now have the ability to utilize the Prism.Container.Extensions as this provides support for abstracting many highly powerful registration methods including factories, and the ability to register a single implementation as a singleton for different services. Additional containers may be added there. Additional documentation for this can be found at https://prismplugins.com

这些是 Prism 团队正式发货的容器

These are containers that ship officially from the Prism team

这是我使用最多的容器.它正在积极开发中,速度非常快,并且与 Prism 的当前版本配合得很好.同样重要的是,当我有疑问或问题时,维护者会非常迅速地解决问题或回答我的问题.Dadhi 也非常擅长主动为 Prism 集成提供增强功能.出于所有这些原因,我继续推荐容器.与 Unity 不同的是,API 往往非常稳定,而且我还没有遇到过将 DryIoc 更新到超出 Prism 特定版本所针对的范围的问题.

This is the container I use and recommend the most. It's under active development, it's very fast, and works well with the current release of Prism. Also important is that when I have had questions or issues the maintainer has been very quick to address the issue or answer the question I had. Dadhi is also very good at proactively providing enhancements for the Prism integration. It's for all of these reasons I continue to recommend the container. Unlike Unity the API tends to be very stable and I have not yet had an issue with updating DryIoc beyond what a particular release of Prism was targeting.

不要与 Unity 游戏开发平台混淆.这是最受欢迎的容器,因为它是 Brian 使用多年的容器,并且它是模板中第一个(并且很长一段时间内唯一)可用的容器.它已经有一段时间没有维护了,但是该项目确实有一个新的维护者.值得注意的是,Unity 5 中有许多重大更改,这使得使用 Prism 6.3 升级到 Unity 5 变得不可能.然而,Prism 7 在所有平台上都更新到了 Unity 5.Unity 在其基准性能方面也处于平均水平.对于从 Prism 6.X 升级到 Prism 7 的用户,请注意您应该卸载对 Unity 或 Common Service Locator 的任何引用,然后更新 Prism.Unity.Forms,它现在面向 Unity.Container NuGet 包而不是 Unity NuGet 包.您还应该注意,针对比 Prism 所针对的更新版本的 Unity 可能会破坏您的应用程序,因为 Unity 引入了许多重大更改,但没有从 Minor Patch 到 Minor Patch 的解释或文档.

Not to be confused with the Unity Game Development Platform. This is the most popular container due to it being the container that Brian has used for years and it being the first (and for a long time only) container available in the Templates. It had gone quite some time without being maintained, however the project does have a new maintainer. It's worth noting that there were a number of breaking changes in Unity 5 which makes upgrading to Unity 5 with Prism 6.3 an impossibility. Prism has however updated to Unity 5 across all platforms in Prism 7. Unity is also about average with regards to its benchmark performance. For those upgrade to Prism 7 from Prism 6.X note that you should uninstall any references to Unity or the Common Service Locator and then update Prism.Unity.Forms which now targets the Unity.Container NuGet package instead of the Unity NuGet package. You should also beware that targeting a newer version of Unity than what Prism is built against may break your application as Unity has introduced a number of breaking changes without explanation or documentation from Minor Patch to Minor Patch.

这些是提供非官方软件包的容器.这些可能对您有用,也可能不适合,风险由您自行承担.

These are containers that have an unofficial package available. These may or may not work for you and are at your own risk.

Prism 需要某些特性,例如 Microsoft DependencyInjection 模式不支持的可变性和命名服务.然而,有一种实现可以为 Prism.Container.Extensions 项目中的所有 Prism 应用程序/平台提供普遍支持.

Prism requires certain features such as mutability and named services which are not supported by the Microsoft DependencyInjection pattern. There is however an implementation that provides universal support for all Prism applications/platforms from the Prism.Container.Extensions project.

虽然这些容器多年来已在许多项目中使用,但 Prism 团队不再支持以下容器.

While these containers have been used in many projects over the years the following containers are No Longer supported by the Prism team.

尽管很受欢迎,但我通常不建议使用这种容器.人们似乎对 API 非常困惑.在 Prism 6.3 中,它的实现非常糟糕.可悲的是,为了提高性能,Autofac 团队非常坚决地决定让容器不可变.从 Prism 7.1 开始,Prism 正式停止支持此容器.

Despite being popular, is a container I generally would advise against using. People seem to be very confused by the API. In Prism 6.3 it suffered from a really poor implementation. Sadly the Autofac team decided very strongly in order to improve performance that they would make the container immutable. Prism officially dropped support for this container as of Prism 7.1.

Ninject 长期以来一直是使用最少的容器之一.随着团队转向支持 netstandard,它在 7.0 中从 Prism.Forms 中删除.虽然 Ninject 3.3 在技术上确实附带了 netstandard2.0 api,但它与 Xamarin 目标不兼容.2017 年 11 月发布的最新 3.3 版本和 2016 年 8 月发布的最新 4.0 预览版目前也处于非常不健康的状态.

Ninject was long one of the least utilized container. This was dropped from Prism.Forms in 7.0 as the team moved to support netstandard. While Ninject 3.3 does technically ship with a netstandard2.0 api, it's is not compatible with Xamarin targets. It is also currently in a very unhealthy state having the latest 3.3 release from November 2017 and the latest 4.0 preview from August 2016.

值得注意的是,从 Prism 7 的 Preview 5 开始,我们已经对容器进行了抽象.这最终将使在您选择的容器之间切换变得更加容易,因为 API 在如何注册您的服务和视图方面完全相同.您仍然可以通过扩展方法访问 Container,在 Autofac 的情况下可以访问 ContainerBuilder,以便您可以完成更复杂的注册.

Well worth noting is that starting with Preview 5 of Prism 7 we have abstracted the containers. This will ultimately make it far easier to switch between the container of your choosing as the API is exactly the same with regards to how to register your services and Views. You will still have access to the Container and in the case of Autofac the ContainerBuilder through extension methods, so that you can accomplish more complex registrations.

// Prism 6.X way of Registering Services
protected override void RegisterTypes()
{
    // Container Specific Registrations

    // Autofac
    Builder.RegisterType<DebugLogger>().As<ILoggerFacade>().SingleInstance();

    // DryIoc
    Container.Register<ILoggerFacade, DebugLogger>(reuse: Reuse.Singleton,
                                                   ifAlreadyRegistered: IfAlreadyRegistered.Replace);

    // Ninject
    Container.Bind<ILoggerFacade>().To<DebugLogger>().InSingletonScope();

    // Unity
    Container.RegisterType<ILoggerFacade, MCAnalyticsLogger>(new ContainerControlledLifetimeManager());
}

// Unified API in Prism 7
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterSingleton<ILoggerFacade, DebugLogger>();
}

同样重要的是要记住,虽然 Prism 的 IoC 抽象使拥有更统一的 API 变得更容易,但这并没有消除您直接与底层容器交互的能力.要访问底层容器,您只需调用 GetContainer 扩展方法,您就可以执行 Prism 的 IoC 抽象不直接支持的任何更复杂的操作.

It is also important to remember that while Prism's IoC abstractions have made it easier to have a more unified API, this does not remove your ability to interact directly with the underlying Container. To access the underlying container you simply need to call the GetContainer extension method and you will be able to perform any more complex action that is not directly supported by Prism's IoC abstractions.

Prism 7.2 引入了一些围绕 IoC 抽象的 API 更改.值得注意的是,总的来说,这些更改不应影响大多数用户.但是,如果使用引用旧版 Prism 的其他库,您可能会遇到二进制不兼容问题.

Prism 7.2 has introduced some API changes around the IoC Abstractions. Notably by and large these changes should not affect most users. However you may experience binary incompatibilities if using other libraries that reference older versions of Prism.

IoC 的变化包括:

  • 流畅的 API
  • 能够检查服务是否已注册
  • 添加了允许临时服务和单例服务成为命名服务的方法
  • 添加了使用指定实例解析服务的功能.

这篇关于Prism.Forms 哪个 IoC 容器更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆