什么是匕首?以及为什么要使用它? [英] What is Dagger? And why we use it?

查看:114
本文介绍了什么是匕首?以及为什么要使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的Android项目中使用Dagger,以及使用Dagger的主要目的是什么?我在Google上进行了搜索,但不清楚.因此,请尽可能提供最佳解决方案和示例.

How to use Dagger in my Android project and what is the main purpose of using it? I searched it on google but I am not clear about it.So please give me the best solution and an example also if possible.

推荐答案

许多Android应用都依赖于实例化对象,这些对象通常需要其他依赖项.例如,可以使用诸如翻新之类的网络库来构建Twitter API客户端.要使用此库,您可能还需要添加诸如Gson之类的解析库.另外,实现身份验证或缓存的类可能需要访问共享的首选项或其他公共存储,需要首先实例化它们并创建一个固有的依赖链.

Many Android apps rely on instantiating objects that often require other dependencies. For instance, a Twitter API client may be built using a networking library such as Retrofit. To use this library, you might also need to add parsing libraries such as Gson. In addition, classes that implement authentication or caching may require accessing shared preferences or other common storage, requiring instantiating them first and creating an inherent dependency chain.

Dagger 2为您分析这些依赖关系,并生成代码以帮助将它们连接在一起.尽管还有其他Java依赖项注入框架,但其中许多依赖XML的局限性,在运行时需要验证依赖项问题或在启动过程中会导致性能下降.Dagger 2完全依靠使用Java注释处理器和编译时检查来分析和验证依赖关系.它被认为是迄今为止构建的最有效的依赖项注入框架之一.

Dagger 2 analyzes these dependencies for you and generates code to help wire them together. While there are other Java dependency injection frameworks, many of them suffered limitations in relying on XML, required validating dependency issues at run-time, or incurred performance penalties during startup. Dagger 2 relies purely on using Java annotation processors and compile-time checks to analyze and verify dependencies. It is considered to be one of the most efficient dependency injection frameworks built to date.

优势

以下是使用Dagger 2的其他优点的列表:

Here is a list of other advantages of using Dagger 2:

  • 简化对共享实例的访问.正如ButterKnife库可以更轻松地定义对视图,​​事件处理程序和资源的引用一样,Dagger 2提供了一种简单的方法来获取对共享实例的引用.例如,一旦我们在Dagger中声明了MyTwitterApiClient或SharedPreferences之类的单例实例,就可以使用简单的@Inject注释声明字段:

  • Simplifies access to shared instances. Just as the ButterKnife library makes it easier to define references to Views, event handlers, and resources, Dagger 2 provides a simple way to obtain references to shared instances. For instance, once we declare in Dagger our singleton instances such as MyTwitterApiClient or SharedPreferences, we can declare fields with a simple @Inject annotation:

public class MainActivity extends Activity {

   @Inject MyTwitterApiClient mTwitterApiClient;
   @Inject SharedPreferences sharedPreferences;

   public void onCreate(Bundle savedInstance) {
       // assign singleton instances to fields
       InjectorClass.inject(this);
   }
}

  • 轻松配置复杂的依赖项.通常以隐式顺序创建对象.Dagger 2遍历依赖关系图,并生成易于理解和跟踪的代码,同时还避免了编写大量通常需要手工编写以获得引用并将其作为依赖项传递给其他对象的模板代码.它还可以帮助简化重构,因为您可以专注于要构建的模块,而不是专注于创建模块的顺序.

  • Easy configuration of complex dependencies. There is an implicit order in which your objects are often created. Dagger 2 walks through the dependency graph and generates code that is both easy to understand and trace, while also saving you from writing a large amount of boilerplate code you would normally need to write by hand to obtain references and pass them to other objects as dependencies. It also helps simplify refactoring, since you can focus on what modules to build rather than focusing on the order in which they need to be created.

    更容易的单元和集成测试因为为我们创建了依赖图,所以我们可以轻松地交换出做出网络响应的模块并模拟这种行为.

    Easier unit and integration testing Because the dependency graph is created for us, we can easily swap out modules that make network responses and mock out this behavior.

    范围内的实例您不仅可以轻松管理可以持续整个应用程序生命周期的实例,还可以利用Dagger 2定义寿命较短的实例(即绑定到用户会话,活动生命周期等).

    Scoped instances Not only can you easily manage instances that can last the entire application lifecycle, you can also leverage Dagger 2 to define instances with shorter lifetimes (i.e. bound to a user session, activity lifecycle, etc.).

    这篇关于什么是匕首?以及为什么要使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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