简体   繁体   English

android数据绑定的优缺点是什么?

[英]What are the pros and cons of android data-binding?

Both of my colleague and I have experience in MVVM of Web App, while we are new to native android development.我和我的同事都有 Web App MVVM 的经验,而我们是原生 android 开发的新手。 Now we have contrary opinions about android data-binding -- I'm a fan of it while he is not.现在我们对 android 数据绑定有相反的看法——我是它的粉丝,而他不是。

My Arguments:我的论点:

  • Reduces boilerplate code which in turns brings减少样板代码,这反过来又带来
    • Less coupling减少耦合
    • Stronger readability更强的可读性
  • Powerful, easy to implement custom attribute and custom view功能强大,易于实现自定义属性和自定义视图
  • Even faster than findViewById ( details )甚至比 findViewById 还要快( 详情

His Arguments:他的论点:

  • The auto-generated .class increases app size.自动生成的 .class 会增加应用程序的大小。
  • Harder to debug更难调试

I've made some investigation but there are not many discussions about it.我已经进行了一些调查,但关于它的讨论并不多。 Now I want to collect the pros and cons of android data-binding.现在我想收集android数据绑定的优缺点。

Aspects of discussion include but are not limited to:讨论的方面包括但不限于:

  • unit test单元测试
  • app size应用大小
  • performance表现
  • learning curve学习曲线
  • readability可读性
  • coupling耦合

I will comment on your arguments first then I will state my opinion:我先评论你的论点,然后我再说一下我的观点:

1.Remove boilerplate code - it will remove some it will just move some in the xml or it will require additional classes. 1.删​​除样板代码 - 它将删除一些它只会在xml移动一些或者它需要额外的类。 So you have to be careful and balance the use of data binding.所以你必须小心和平衡数据绑定的使用。

2.Stronger readability - depends if you are a new developer then you may find it easy to learn it but if you previously worked on android you will need extra time to learn it. 2.更强的可读性 - 取决于你是否是一个新的开发者,那么你可能会发现它很容易学习,但如果你以前在 android 上工作过,你将需要额外的时间来学习它。

3.Powerful - the code has more power, you can implement whatever you like in code. 3.强大——代码更强大,你可以在代码中实现你喜欢的任何东西。 Think about it like this, everything you implement using data binding has a code equivalent (it might be longer and more code to write), but the revers is not valid.像这样想,您使用数据绑定实现的所有内容都有一个等效的代码(可能会更长,需要编写更多代码),但反过来是无效的。

4.Even faster than findViewById - comparing the speed between these two, in my opinion is useless, you will never notice the difference, if you see some difference, then one of the implementation is wrong. 4.甚至比findViewById还要快——比较这两者的速度,在我看来是没用的,你永远不会注意到差异,如果你看到一些差异,那么其中一个实现是错误的。

5.Auto generated class - it's true it will increase the app size, but again only if you have tons of it it will matter. 5.自动生成的类 - 它确实会增加应用程序的大小,但同样只有当你有大量的它才会重要。 It's true that on the android dev web site they state that it's kind of bad to use libraries that create autogenerated code or annotations that will generate extra code.确实,在 android 开发网站上,他们声明使用创建自动生成代码的库或将生成额外代码的annotations有点不好。

6.Hard to debug - depends, like readability, of what you are used to, heck debugging is hard either way for some problems, and you will get better by debugging not by using a different library. 6.难以调试 - 与可读性一样,取决于您所习惯的,对于某些问题,无论哪种方式都很难调试,并且通过调试而不是使用不同的库,您会变得更好。

So this is pure my opinion, I've developed many apps using different libraries and different approaches, and they all had pros and cons, but what I've learn: balance everything, don't use tons of libraries, don't waste time implementing things that are implemented already and work well, don't "decouple everything", don't "couple" everything, don't use code only, don't try to "generate" everything.所以这纯粹是我的观点,我使用不同的库和不同的方法开发了许多应用程序,它们都有优点和缺点,但我学到了什么:平衡一切,不要使用大量的库,不要浪费花时间实现已经实现并且运行良好的东西,不要“解耦一切”,不要“耦合”一切,不要只使用代码,不要试图“生成”一切。

I think it's quite wrong, and you can get a wrong idea, if you ask for 'pros & cons' about some library/implementation, because usually it won't be impartial, you will get a lot of pros from somebody who used the library in a specific way and it worked and others will give you cons because they used different and it didn't work.我认为这是完全错误的,如果你问一些库/实现的“利弊”,你可能会得到一个错误的想法,因为通常它不会是公正的,你会从使用过的人那里得到很多优点图书馆以特定的方式工作并且它起作用,而其他人会给您带来缺点,因为他们使用不同的方法并且不起作用。

So in conclusion, I think you should check what the library can do for you and what can't do for you and decide if it's good for your setup.因此,总而言之,我认为您应该检查库可以为您做什么以及不能为您做什么,并确定它是否适合您的设置。 In other words, you should decide if a library is good for you not other people ;).换句话说,你应该决定一个图书馆是否适合你而不是其他人;)。

Update - 8 August 2018更新 - 2018 年 8 月 8 日

First of all I still stand with my initial conclusion, balance is the key in these kind of situations, but in my case, data-binding speed-up a little bit the development process and also improved it.首先,我仍然坚持我最初的结论,在这种情况下平衡是关键,但在我的情况下,数据绑定稍微加快了开发过程,也改进了它。 Here are a few new points that you should all think about.这里有一些新的观点,你们都应该考虑一下。

  1. Testing the UI -- with data-binding it's much more easy to test the UI, but data-binding it's not enough for that, you also need a good architecture and using the Google suggested architecture will show the actual power of data-binding.测试用户界面——使用数据绑定测试用户界面要容易得多,但是数据绑定还不够,你还需要一个好的架构,使用谷歌建议的架构将显示数据绑定的实际威力。

  2. The most visible changes were provided for points 2 & 5 from my original answer.从我的原始答案中,为第 2 点和第 5 点提供了最明显的变化。 It kind of was easier to read the code after we decided to use data-binding, and the most important thing here is: we as a team decided that we will use data-binding and after that, we kind of expected to have most of the trivial and basic UI setup in the XML file.在我们决定使用数据绑定之后,代码更容易阅读,这里最重要的事情是:我们作为一个团队决定我们将使用数据绑定,在那之后,我们有点期望拥有大部分XML 文件中的琐碎和基本 UI 设置。

For the debugging part, here's a little bit tricky, Android Studio has a lot to improve on the errors and autocomplete for the data-binding but the most common errors you'll get them after the first 2-3 occurrences.对于调试部分,这里有点棘手,Android Studio 在数据绑定的错误和自动完成方面有很多改进,但最常见的错误是在前 2-3 次发生后出现。 Also I've learned that a "clean project" form time to time, helps A LOT.我还了解到,一个“干净的项目”不时形成,有很多帮助。

  1. Another point that you'll have to take in consideration is the project configuration to use data-binding, right now AS (3.1) supports by default data-binding (just set a flag in graddle) for Java, but I had some issues with Kotlin, after a bit of search here on SO, I managed to fix everything.您必须考虑的另一点是使用数据绑定的项目配置,现在 AS (3.1) 默认支持 Java 的数据绑定(只需在 graddle 中设置一个标志),但我遇到了一些问题Kotlin,在 SO 上进行了一些搜索后,我设法解决了所有问题。

As a second conclusion (from my original post), if you can and the project deadline/requirements/etc allows you to try data-binding, go for it it will worth (unless you do some really stupid stuff :)) ).作为第二个结论(来自我的原始帖子),如果可以并且项目截止日期/要求/等允许您尝试数据绑定,那么去尝试它是值得的(除非您做了一些非常愚蠢的事情:)))。

Even if i like danypata's answer i would like to add/edit some of his statements to android databinding.即使我喜欢 danypata 的回答,我也想将他的一些语句添加/编辑到 android 数据绑定中。

1.Remove boilerplate code - As written in danypatas answer it removes some code and adds some code somewhere else like in layouts. 1.删除样板代码- 正如在 danypatas 中写的那样,它删除了一些代码并在其他地方添加了一些代码,比如在布局中。 That doesnt mean that the boilercode isnt reduced because usually it is reduced.这并不意味着锅炉代码不会减少,因为通常它会减少。

For example you may want to create a bindingadapter, which handles several custom arrayadapters for your spinner/recyclerview/listview/.. but requires only one simple adapter.例如,您可能想要创建一个 bindingadapter,它可以为您的 spinner/recyclerview/listview/.. 处理多个自定义数组适配器,但只需要一个简单的适配器。 You may want to use the adapter in your layout by using eg您可能希望在布局中使用适配器,例如

app:myCoolAdaptersData="@{model.mydata}"

Now you can create your generic adapter and (re)use your bindingadapter in all your layouts instead of using for example:现在您可以创建通用适配器并(重新)在所有布局中使用您的 bindingadapter 而不是使用例如:

ListView lv = findViewById(...);
CoolGenericAdapter<MyModel> coolAdapter = new CoolGenericAdapter<>(...);
lv.setAdapter(coolAdapter);

This is just one simple example which recudes the code alot in larger projects.这只是一个简单的例子,它在较大的项目中减少了大量的代码。 Another sample to recude code is, that you bind your model to your layout.另一个减少代码的示例是,将模型绑定到布局。 Updating field-values of your model usually updates your model aswell (if its at least a BaseObservable/ObservableField).更新模型的字段值通常也会更新模型(如果它至少是 BaseObservable/ObservableField)。

That means that you dont need to find all your views, update your views, update your models, ...这意味着您不需要查找所有视图、更新视图、更新模型……

2.Stronger readability - The extra time spent for learning databinding doesnt really matter. 2.更强的可读性- 学习数据绑定所花费的额外时间并不重要。 Since the layouts are not really different except that you wrap them into a layout tag and put your namespaces there, it doesnt really differs from "regular" layouts.由于布局并没有真正不同,只是将它们包装到布局标记中并将命名空间放在那里,因此它与“常规”布局并没有真正的不同。 Using bindingadapters and accessing the model in the layout may take some time, but usually you can start beginning with the basics which are easy and beautiful to use aswell.使用 bindingadapters 和访问布局中的模型可能需要一些时间,但通常您可以从基础知识开始,这些基础知识也易于使用且美观。 Learning new stuff always takes time, but you will easy overhaul the time when using databinding after a while.学习新东西总是需要时间,但在使用数据绑定一段时间后,你会很容易地调整时间。

3.Powerful - Yes, its very powerful. 3.Powerful - 是的,它非常强大。 Its easier to reuse existing code, reuse existing bindingadapters and may lead to more generated code but thats not always true.重用现有代码、重用现有绑定适配器更容易,并可能导致生成更多代码,但并非总是如此。 For example you may create multiple adapters within several classes instead of creating one bindingadapter, it may be hard to "optimize" it later.例如,您可以在多个类中创建多个适配器而不是创建一个绑定适配器,以后可能很难对其进行“优化”。 Optimizing the Bindingadapter means that it gets updated everywhere.优化 Bindingadapter 意味着它会随处更新。 Optimizing may decrease the "lines of code" since the boilerplace is reduced anyway.优化可能会减少“代码行数”,因为无论如何都会减少样板间。

I agree to 4. and 5.我同意 4. 和 5.

6. Hard to Debug Since AS 3.0+ outputs useful hints like syntax issues in your layout (line number and file) its easy to debug databinding generated code. 6. 难以调试由于 AS 3.0+ 会输出有用的提示,例如布局中的语法问题(行号和文件),因此很容易调试数据绑定生成的代码。 If you have problems finding the issue you may also want to check for errors in the generated code.如果您在查找问题时遇到问题,您可能还需要检查生成的代码中是否存在错误。 Some librarys like dagger 2 or android architecture library may confuse you because the error lines doesnt match with the real "error".一些库如 dagger 2 或 android 架构库可能会让你感到困惑,因为错误行与真正的“错误”不匹配。 This is due generated code by other annotation processors.这是由于其他注释处理器生成的代码。 If you know that those annotation processors may get in trouble with databindings error outputs, you can easy fix that.如果您知道那些注释处理器可能会遇到数据绑定错误输出的问题,那么您可以轻松解决这个问题。

7. Unit Testing Its possible like if you dont use databinding by using executePendingBindings. 7. 单元测试这可能就像你不使用数据绑定通过使用 executePendingBindings。

8. Readability Readability may be better without databinding. 8. 可读性没有数据绑定的可读性可能会更好。 Since you put some business logic into your layout, some into your real code, it may lead to spaghetti-code.由于您将一些业务逻辑放入布局中,将一些放入实际代码中,因此可能会导致意大利面条式代码。 Another problem is that using lambdas in your layout may be very confused if the "layout-designer" doesnt know which param may be used.另一个问题是,如果“布局设计师”不知道可以使用哪个参数,则在布局中使用 lambda 可能会非常混乱。

Another very big problem is that bindingadapter can be everywhere.另一个非常大的问题是 bindingadapter 可以无处不在。 Using BindingAdapter annotation generates the code.使用 BindingAdapter 注释生成代码。 That means that using this in your layout may lead to problems to find the proper code.这意味着在您的布局中使用它可能会导致无法找到正确的代码。 If you want to update a bindingadapter you need to "find" it.如果要更新 bindingadapter,则需要“找到”它。

When should you use what?什么时候应该使用什么? For larger projects it is a really good idea to use databinding together with the mvvm or mvp pattern.对于较大的项目,将数据绑定与 mvvm 或 mvp 模式一起使用是一个非常好的主意。 This is a really clean solution and very easy to extend.这是一个非常干净的解决方案,并且非常容易扩展。 If you just want to create a small simple application you'r fine using MVC Pattern without databinding.如果您只想创建一个简单的小型应用程序,则可以使用没有数据绑定的 MVC 模式。 If you have existing generic bindingadapters which can be used from other projects you may want to use databinding, because its easy to reuse this code.如果您已有可从其他项目使用的通用绑定适配器,您可能希望使用数据绑定,因为它易于重用此代码。

I am working on a huge Android project and the team has decided to phase out Data Binding library.我正在处理一个巨大的 Android 项目,团队决定逐步淘汰数据绑定库。 Why?为什么? The primary reason is that it is exacerbating build time(10+ mins), by generating a lot of classes in the build process.主要原因是它在构建过程中生成了大量类,从而加剧了构建时间(10 分钟以上)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM