本文介绍了Transfuse如何与Dagger比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我想决定是否使用Transfuse或Dagger进行Android依赖项注入。我从来没有使用Transfuse,并有Dagger的基本知识。非常感谢。I'm trying to decide whether to use Transfuse or Dagger for Android dependency injection. I've never used Transfuse, and have basic knowledge of Dagger. Thanks much.推荐答案首先,我是To start, I am the primary author of Transfuse thus this answer may be a bit slanted in that direction. Transfuse和Dagger都处理依赖注入/控制反转Android以类似的方式。两者都在编译时使用注释处理,通过 JSR269 生成代码,支持DI / IOC功能。这允许他们避免昂贵的基于运行时反射的分析,通常与在非Android Java中发现的DI容器相关联。在没有详细描述的情况下,Dagger和Transfuse以显着不同的方式处理代码生成,这反映在库的特性中。此外,Transfuse和Dagger都使用常见的 JSR330 注释(@Inject,Provider,等等)。Both Transfuse and Dagger handle Dependency Injection / Inversion of Control for Android in similar ways. Both use Annotation Processing at Compile time via JSR269 to generate code the supports the DI/IOC functionality. This allows them to avoid the costly runtime reflection-based analysis typically associated with DI containers found in non-Android Java. Without going into the specifics, Dagger and Transfuse do approach code generation in significantly different ways, which is reflected in the features of the libraries. Also, Transfuse and Dagger both use the common JSR330 annotations (@Inject, Provider, etc). This means they both follow a Guice-style injection scheme.以下是在Dagger中创建对象图的方法:Here's how you create an object graph in Dagger:public class DaggerActivity extends Activity { @Inject Example example; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ObjectGraph.create().inject(this); //do something else... }} Transfuse中的等效代码使用其@Factory功能:The equivalent code in Transfuse uses its @Factory functionality:@Factorypublic interface Injector { Example get();}public class TransfuseActivity extends Activity { Example example; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); example = Factories.get(Injector.class).get(); //do something else... }} Transfuse是用于以下方式使用POJO组件,生命周期事件等:Transfuse is meant to be used in the following way, however, utilizing POJO components, lifecycle events, etc:@Activitypublic class TransfuseActivity{ @Inject Example example; @OnCreate public void doSomethingElse(){ //do something else... }}以下是Transfuse和Dagger中DI引擎的一些细微差别:Here's some little differences in the DI engine in Transfuse and Dagger: Transfuse支持(以及它可以)循环依赖,Dagger有意在此情况下抛出异常。Transfuse符合JSR330,Dagger特别不支持。 Dagger开发人员想要在简单性方面犯错,避免方法注入允许他们避免一些混乱的情况( link )。 Dagger有一个基于反射的引擎,用于未生成代码的情况。 Transfuse不会注入到私有字段,构造函数和方法中(由于反射开销而不一定推荐使用) )。 Dagger在这种情况下抛出异常。 Dagger以非常直接的方式使用模块,反映了Guice的功能。每次创建对象图时,都可以选择使用Module类来配置它,例如: ObjectGraph.create(new DripCoffeeModule())。 Transfuse的配置模块有点不同,因为它在编译时合并到应用程序中。 Transfuse中的每个模块都是该项目的全局变量(这在Transfuse的未来版本中可能会发生变化,但是对于Transfuse的使用而言并不是一个问题)。 Dagger中的单例Transfuse supports (as well as it can) cyclic dependencies, Dagger purposefully throws an exception in this case.Transfuse satisfies JSR330, Dagger specifically does not. The Dagger developers wanted to err on the side of simplicity, avoiding method injection allowed them to avoid a handful of confusing cases (link).Dagger has a reflection based engine for cases where code was not generated. Transfuse does not and requires code to be generated (annotation processor to run) in order to work.Transfuse will inject into private fields, constructors, methods (not necessarily recommended because of the reflection overhead). Dagger throws an exception in this case.Dagger uses Modules in a very direct way, mirroring the capabilities of Guice. Each time you create an object graph, you are given the option to configure it with a Module class, ie: ObjectGraph.create(new DripCoffeeModule()). Transfuse's configuration module is a bit different as it is incorporated into the application at compile time. Each Module in Transfuse is global to the project (this may change in future versions of Transfuse, but it has not been an issue for the use of Transfuse yet).Singletons in Dagger are per-object-graph where Singletons in Transfuse are global to the application. Dagger和Transfuse的最大区别在于Dagger专注于一个简单的依赖注入库,而Transfuse的重点是使Android更好的API使用性能敏感技术The big difference between Dagger and Transfuse is that Dagger focused on being a simple Dependency Injection library, while Transfuse's focus is to "make Android a better API using performance sensitive techniques" Transfuse支持这些功能以及DI:Transfuse supports these capabilities as well as DI: / li> 清单管理 Roboguice / Butterknife样式注入 轻量级事件系统(@Observes,@OnCreate等) AOPPOJO ComponentsManifest ManagementRoboguice/Butterknife style injectionsLightweight event system (@Observes, @OnCreate, etc)AOP我建议如果您有兴趣,请提供Transfuse a尝试。就我个人而言,我很想听听你的经验与Dagger的对比。我们有邮寄名单,您可以在其中与社区分享,并通过文档。I'd recommend that if you're interested, give Transfuse a try. Personally, I'd love to hear about your experience contrasting it with Dagger. We have a mailing list where you can share with the community and pretty through documentation on the website. 这篇关于Transfuse如何与Dagger比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-07 15:22