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

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

问题描述

我正在开始一个新的Prism.Forms项目,我想知道各种IoC 容器(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

这些是棱镜团队正式运送的集装箱

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多年来使用的容器,并且是Templates中第一个(也是很长时间)可用的容器.它已经花了很长时间没有得到维护,但是该项目确实有一个新的维护者.值得注意的是,Unity 5中发生了许多重大变化,这使得不可能通过Prism 6.3升级到Unity 5.但是,Prism已在Prism 7中的所有平台上更新到Unity5.Unity在基准性能方面也处于中等水平.对于从Prism 6.X升级到Prism 7的用户,请注意,您应该卸载对Unity或Common Service Locator的所有引用,然后更新Prism.Unity.Forms,该目标现在面向Unity.Container NuGet软件包而不是Unity NuGet软件包.您还应该注意,针对Unity而不是针对Prism构建的版本,将可能破坏您的应用程序,因为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变得更加容易,但这并不能消除您直接与基础Container交互的能力,这一点也很重要.要访问基础容器,您只需调用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天全站免登陆