如何在自定义视图或其他 android 类中使用 AndroidInjection 类? [英] How do I use AndroidInjection class in custom views or other android classes?

查看:31
本文介绍了如何在自定义视图或其他 android 类中使用 AndroidInjection 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android 特定模式的问题是,如果您使用他们的 AndroidInjection 类,则除了 Activities/Fragments 之外,成员无法注入其他对象/custom 视图/适配器,应用程序组件除外.这是因为您无法获得用于注入 Activities/FragmentsSubcomponent (AndroidInjector) 的引用.这使得注入对话框(如果您使用 DialogFragments).

My issue with the Android-specific pattern is, if you use their AndroidInjection class, there is no way to members inject other objects besides Activities/Fragments/custom views/adapters, except with the Application Component. This is because you cannot get a reference the the Subcomponent (AndroidInjector) used to inject Activities/Fragments. This makes injecting Dialogs (if you use DialogFragments).

AndroidInjection 类似乎只支持核心 Android 类型.

The AndroidInjection class seems to support just the core Android types.

推荐答案

下面的内容不是对您问题的回答,而是解释您根本不应该问这个问题的原因.

What follows is not an answer to your question, but an explanation why you shouldn't be asking this question at all.

通常应该避免注入自定义Views.原因在这篇文章中列出.

You should avoid injections into custom Views in general. The reasons for this are listed in this article.

在这种情况下使用方法注入的优点[注入自定义视图]是:

Advantages of using Method Injection in this case [injection into custom Views] are:

  • 依赖项需要从顶级组件(活动或片段)传播
  • 方法注入不会为违反单一职责原则打开大门
  • 不依赖于框架
  • 更好的性能

第一个优势可能会让人感到意外,因为从顶级组件比向字段添加注释更难,并且涉及更多样板代码.这肯定是件坏事吧?在这种情况下不是.事实上,有两个好的方面与这种依赖的传播.首先是依赖将在顶级组件中可见.因此,只要看在例如Fragment 的字段,代码的读者会立即了解此 Fragment 显示图像.这样的优化可读性使系统更易于长期维护学期.其次,使用子类的用例并不多查看需要额外的依赖项.事实上,你需要实际工作以提供这些依赖项会给你一些是时候考虑提供它们是否是一个好的设计决策开始.

The first advantage might come as a surprise because propagation from top level component is harder than adding annotation to fields, and involves more boilerplate code. This is surely a bad thing, right?. Not in this case. In fact, there are two good aspects associated with such a propagation of dependencies. First of all, the dependencies will be visible at the top level component. Therefore, just by looking at e.g. Fragment‘s fields, the reader of the code will immediately understand that this Fragment shows images. Such optimizations for readability makes the system more easily maintainable in the long term. Secondly, there are not many use cases in which sub-classes of View need additional dependencies. The fact that you need to actually work in order to provide these dependencies will give you a bit of time to think about whether providing them is a good design decision to start with.

第二个优势与协同建设有关.你你自己可能是非常有经验的软件工程师,但你会可能还有经验不足的队友.或者有可能总有一天你会离开这个项目,接手的人会不如你.通过使用一个注入一个单一的依赖框架,你基本上为其他注入打开了一扇门.想象在自定义视图中需要来自 SharedPreferences 的一些数据为了例如修复一个错误.经验不足的开发人员之一可能会认为这是注入 SharedPreferences 的好方法直接进入自定义视图.这样做违反了单一职责原则,但该开发人员可能甚至不知道这样的概念.因此,从长远来看,这种注入后门"可以降低设计质量并导致长时间的调试会话.

The second advantage is related to collaborative construction. You might be very experienced software engineer yourself, but you’ll probably have also less experienced teammates. Or it is possible that you’ll leave the project one day, and the guy who will take over will not be as good as you. By injecting one single dependency using a framework, you basically open a door for other injections. Imagine that some data from SharedPreferences becomes required in custom View in order to e.g. fix a bug. One of the less experienced developers might decide that it is a good approach to inject SharedPreferences into custom View directly. Doing this violates Single Responsibility Principle, but that developer might not even be aware of such a concept. Therefore, in the long term, such injection "backdoors" can reduce design quality and lead to long debug sessions.

在自定义视图中使用方法注入的第三个优点是你没有将 View 与依赖注入框架结合起来.只是想象几年后你(或其他一些可怜的人)需要更换框架.事实上,你可能有几十个开始的活动和片段会让你的生活变得悲惨.如果您要处理额外的数十或数百个自定义视图,那么它可能会让你产生自杀的情绪.

The third advantage of using Method Injection with custom Views is that you don’t couple the View to dependency injection framework. Just imagine that few years from now you (or some other poor guy) need to replace the framework. The fact that you’ll probably have tens of Activities and Fragments to start with will make your life miserable. If you’ll have additional tens or hundreds of custom Views to handle, then it might bring you into suicidal mood.

最后(但并非最不重要)的优势是性能.一屏可以包含一个活动、几个片段和数十个自定义视图.使用依赖注入引导这个数量的类框架可能会降低应用程序的性能.尤其是对于基于反射的框架来说是正确的,但即使是 Dagger 也带有一些性能成本.

The last (but not least) advantage is performance. One screen can contain one Activity, several Fragments and tens of custom Views. Bootstrapping this number of classes using dependency injection framework might degrade application’s performance. It is especially true for reflection based frameworks, but even Dagger carries some performance cost.

此外,我建议避免使用涉及 AndroidInjection 类的新注入方法.本视频教程中对此进行了讨论.

In addition, I advice to avoid the new injection method that involves AndroidInjection class. It is discussed in this video tutorial.

这篇关于如何在自定义视图或其他 android 类中使用 AndroidInjection 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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