Dagger 和 ButterKnife Android 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20821148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Dagger and ButterKnife Android
提问by user2511882
Can anyone point out the difference between Daggerand Butterknife? I know that Butterknife is a view injection library and Dagger is a dependency injection library. But the documentation online seems a bit overhead for me. According to Butterknife documentation, you can do non-activity injections as well, which is what Dagger does? Or did I misunderstand something?
谁能指出Dagger和Butterknife之间的区别?我知道 Butterknife 是一个视图注入库,而 Dagger 是一个依赖注入库。但是在线文档对我来说似乎有点开销。根据 Butterknife 文档,您也可以进行非活动注入,这就是 Dagger 的作用?还是我误会了什么?
回答by Pavel Dudka
ButterKnife is targeted to inject views only. Non-activity injection just means that you can provide your own view root to inject views from (like with manually inflated views, etc.).
Dagger is a bit more complicated. It can inject anything you want as long as you specified Module
- class which satisfies those dependencies (alternatively you can use constructor injection).
ButterKnife 仅用于注入视图。非活动注入仅意味着您可以提供自己的视图根以从中注入视图(例如手动膨胀的视图等)。Dagger 有点复杂。只要您指定,它就可以注入您想要的任何内容Module
- 满足这些依赖项的类(或者您可以使用构造函数注入)。
As a bottom line - I would say ButterKnife helps you to avoid all that boilerplate code for creating views (aka (TextView)findViewById(R.id.some_text_view);
. Nothing more. Under the hood it still does all that boring code. So it is not really an injection..
作为底线 - 我会说 ButterKnife 可以帮助您避免所有用于创建视图的样板代码(又名(TextView)findViewById(R.id.some_text_view);
。仅此而已。在幕后它仍然执行所有那些无聊的代码。所以它不是真正的注入..
Also it worth mentioning that Jake Wharton is one of the developers for both those cool libs :)
另外值得一提的是,Jake Wharton 是这两个酷库的开发者之一:)
Here is some useful links:
以下是一些有用的链接:
Dagger sample project on GitHub
Dagger presentation on Devoxx (Jake Wharton)
Devoxx 上的 Dagger 演示(Jake Wharton)
Dagger hangout with Dagger devs
Don't have much of useful ButterKnife links. It really simple and straight forward though, so hopefully you don't need any
没有太多有用的 ButterKnife 链接。不过它真的很简单直接,所以希望你不需要任何
回答by greenspand
Here is a link to the Buterknife documentation. It's very straightforward. However, what the documentation doesn't say but the design of Butter Knife implies is that you can also use Butter Knife in custom views. Just replace "this" with "context" or "getContext" so you determine the scope.
这是 Buterknife 文档的链接。这非常简单。但是,文档没有说但 Butter Knife 的设计暗示您也可以在自定义视图中使用 Butter Knife。只需将“this”替换为“context”或“getContext”即可确定范围。
Link: http://jakewharton.github.io/butterknife/
链接:http: //jakewharton.github.io/butterknife/
I combine Butter Knife, parcelable and easyAdapter for list views in my project. Reason is less boilerplate and with parcelable faster and cleaner parceling. So if you have a lot of ListViews, I recommend this approach.
我将 Butter Knife、parcelable 和 easyAdapter 组合用于我的项目中的列表视图。原因是更少的样板和可打包的更快更干净的打包。所以如果你有很多 ListView,我推荐这种方法。
Links:
链接:
https://github.com/johncarl81/parceler
https://github.com/johncarl81/parceler
回答by naXa
The difference is pretty straightforward: A butter knifeis like a daggeronly infinitely less sharp.
As it is pointed out in the documentation.
回答by serv-inc
@JakeWharton's answers it partially in the comment:
@JakeWharton 在评论中部分回答了这个问题:
TL;DR: They complement each other. Use Butterknife for injecting views, Dagger for the rest.
TL;DR:它们相辅相成。使用 Butterknife 注入视图,其余使用 Dagger。
If you use Dagger to try and inject views you're going to have a very bad time :) Try to think of Butter Knife as a means of binding views rather than injection. I only called it injection because all the RoboGuice converts were complaining about lack of "view injection" with Dagger. It's not really injection at all. Like the answer mentions, it's just a way to reduce boilerplate. Under the hood it's just calling findViewById like you would!
如果您使用 Dagger 尝试注入视图,您将度过一段非常糟糕的时光:) 尝试将 Butter Knife 视为绑定视图而不是注入的一种手段。我之所以称之为注入,是因为所有的 RoboGuice 转换者都抱怨 Dagger 缺乏“视图注入”。这根本不是真正的注射。就像答案提到的那样,这只是减少样板的一种方法。在引擎盖下,它只是像您一样调用 findViewById !
回答by EpicPandaForce
ButterKnife was made to simplify registering click listeners, and to reduce the boilerplate provided by findViewById()
calls.
ButterKnife 旨在简化注册点击侦听器,并减少findViewById()
调用提供的样板文件。
Dagger and Dagger2 are general purpose dependency injection systems.
Dagger 和 Dagger2 是通用的依赖注入系统。
Ignore the text on Guice and MVVM, and read Dependency Injection
and The Android Way
. This pretty much answers what Dagger is meant to do and simplify.
忽略 Guice 和 MVVM 上的文本,并阅读Dependency Injection
和The Android Way
。这几乎回答了 Dagger 的意图和简化。
https://spring.io/blog/2011/08/26/clean-code-in-android-applications
https://spring.io/blog/2011/08/26/clean-code-in-android-applications