-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
用 Dagger 2 实现依赖注入 #1484
用 Dagger 2 实现依赖注入 #1484
Conversation
好久没来了,来一个校对 |
@Zhiw 好哒 |
@@ -31,17 +31,17 @@ public class MainActivity extends Activity { | |||
} | |||
``` | |||
|
|||
* **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|Dependency-Injection-with-Dagger-2#code-generation]] that is both easy to understand and trace, while also saving you from writing the 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. | |||
* **容易配置复杂的依赖**。 对象创建是有隐含顺序的。Dagger 2 浏览依赖图,并且[生成易于理解和追踪的代码](https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2#code-generation)。而且,它可以节约大量的样板代码,使你不再需要手写,手动获取引用并把它们传递给其他对象作为依赖。它也简化了重构,因为你可以聚焦于构建模块本身,而不是它们被创建的顺序。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『容易配置复杂的依赖』=>『轻松配置复杂的依赖关系』
『Dagger 2 浏览依赖图』=>『Dagger 2 遍历依赖关系图』
|
||
An important aspect of Dagger 2 is that the library generates code for classes annotated with the `@Component` interface. You can use a class prefixed with `Dagger` (i.e. `DaggerTwitterApiComponent.java`) that will be responsible for instantiating an instance of our dependency graph and using it to perform the injection work for fields annotated with `@Inject`. See the [[setup guide|Dependency-Injection-with-Dagger-2#setup]]. | ||
### Instantiating the component | ||
Dagger 2 的一个重要特点是它会为标注 `@Component` 的接口生成类的代码。你可以使用带有 `Dagger` (比如 `DaggerTwitterApiComponent.java`) 前缀的类来为依赖图提供实例,并用它来完成用 `@Inject` 注解的域的注入。 参见[[setup guide|Dependency-Injection-with-Dagger-2#setup]]。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『参见[[setup guide|Dependency-Injection-with-Dagger-2#setup]]』=> 『参见 设置』
|
||
#### Dependent Components | ||
* **Dagger 2 同样允许使用带作用域的实例。你需要负责在合适的时机创建和销毁引用。** Dagger 2 对底层实现一无所知。这个 Stack Overflow [讨论](http://stackoverflow.com/questions/28411352/what-determines-the-lifecycle-of-a-component-object-graph-in-dagger-2) 上有更多的细节。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
此处三个部分原文是以列表项呈现,请参考原文,检查一下格式
@@ -380,7 +382,7 @@ public interface GitHubComponent { | |||
} | |||
``` | |||
|
|||
Let's assume this GitHub module simply returns back an API interface to the GitHub API: | |||
假定 Github 模块只是把 API 接口返回给 github API: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『Github』,『github』=>『GitHub』
@@ -415,7 +417,7 @@ public interface NetComponent { | |||
} | |||
``` | |||
|
|||
The final step is to use the `GitHubComponent` to perform the instantiation. This time, we first need to build the `NetComponent` and pass it into the constructor of the `DaggerGitHubComponent` builder: | |||
最终的步骤是用 `GitHubComponent` 进行实例化。这一次,我们需要首先实现 `NetComponent` 并把它传递给 `DaggerGitHubComponent` 建造者的构造方法: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
builder 这里可以不用翻译
|
||
* If you are upgrading Dagger 2 versions (i.e. from v2.0 to v2.5), some of the generated code has changed. If you are incorporating Dagger code that was generated with older versions, you may see `MemberInjector` and `actual and former argument lists different in length` errors. Make sure to clean the entire project and verify that you have upgraded all versions to use the consistent version of Dagger 2. | ||
* 如果你在升级 Dagger 版本(比如从 v2.0 升级到 v 2.5),一些被生成的代码会改变。如果你在集成使用旧版本 Dagger 生成的代码,你可能会看到 `MemberInjector` 和 `actual and former argument lists different in length` 错误。确保你 clean 过整个项目,并且把所有版本升级到和 Dagger 2 相匹配的版本。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『如果你在集成使用旧版本 Dagger 生成的代码』=>『如果你在使用旧版本生成的 Dagger 代码』
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
incorporate本来就有集成的意思。
|
||
If you're not familiar with Dependency Injection, watch [this](https://www.youtube.com/watch?v=IKD2-MAkXyQ) quick video. | ||
如果你不熟悉依赖注入,看看[这个](https://www.youtube.com/watch?v=IKD2-MAkXyQ)短视频。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
链接前后空格的问题,统一即可
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
英文前后加空格,中文不加。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
校对完毕
@sqrthree 校对认领 |
@mnikn 好哒 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tanglie1993 @sqrthree 校对完成,翻译得挺好,校对中主要是针对一些语句提供润色建议。
|
||
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|Consuming-APIs-with-Retrofit]]. To use this library, you might also need to add parsing libraries such as [[Gson|Leveraging-the-Gson-Library]]. In addition, classes that implement authentication or caching may require accessing [[shared preferences|Storing-and-Accessing-SharedPreferences]] or other common storage, requiring instantiating them first and creating an inherent dependency chain. | ||
很多 Android 应用依赖于一些含有其它依赖的对象。例如,一个 Twitter API 客户端可能需要通过 [Retrofit](https://github.com/codepath/android_guides/wiki/Consuming-APIs-with-Retrofit) 之类的网络库被构建。要使用这个库,你可能还需要添加 [Gson](https://github.com/codepath/android_guides/wiki/Leveraging-the-Gson-Library) 这样的解析库。另外,实现认证或缓存的库可能需要使用 [shared preferences](https://github.com/codepath/android_guides/wiki/Storing-and-Accessing-SharedPreferences) 或其它通用存储方式。这就需要先把它们实例化,并创建一个隐含的依赖链。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
之类的网络库被构建 -> 之类的网络库来构建
|
||
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](http://google.github.io/dagger/) relies purely on using Java [annotation processors](https://www.youtube.com/watch?v=dOcs-NKK-RA) 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 为你解析这些依赖,并生成把它们绑定在一起的代码。也有很多其它的 Java 依赖注入框架,但它们中很多个是有缺陷的,比如依赖 XML,需要在运行时验证依赖,或者在起始时造成性能负担。 [Dagger 2](http://google.github.io/dagger/) 纯粹依赖于 Java [注解解析器](https://www.youtube.com/watch?v=dOcs-NKK-RA)以及编译时检查来分析并验证依赖。它被认为是目前最高效的依赖注入框架之一。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
但它们中很多个是有缺陷的 -> 但它们中大多数是有缺陷的
|
||
Make sure to [[upgrade|Getting-Started-with-Gradle#upgrading-gradle]] to the latest Gradle version to use the `annotationProcessor` syntax: | ||
确保[升级](https://github.com/codepath/android_guides/wiki/Getting-Started-with-Gradle#upgrading-gradle) 到最迟的 Gradle 版本以使用最新的 `annotationProcessor` 语法: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最迟的 Gradle 版本 -> 最新的 Gradle 版本
@tanglie1993 两位校对者都已经校对好了~ 可以来根据校对意见进行调整了哈 ┏ (゜ω゜)=☞ |
好了。 |
已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。 |
发布到专栏里啦。不用分享了。你可以看下 wiki 里面的说明。 |
?什么专栏?没找到。 |
所谓“掘金原创专栏”是这个吗? |
我的意思是你需要把这篇文章发布到专栏里,然后把链接发给我,取代之前的分享流程。 |
No description provided.