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

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

问题描述

我对Android特定模式的问题是,如果使用他们的AndroidInjection类,除了应用程序组件之外,没有成员可以注入除Activities/Fragments/custom views/adapters之外的其他对象. .这是因为您无法获得用于注入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:

  • 需要从顶级组件(活动或片段)传播依赖项
  • 方法注入不会为违反单一责任原则敞开大门
  • 不依赖框架
  • 更好的性能
  • Dependencies will need to be propagated from top level component (Activity or Fragment)
  • Method Injection does not open door to Single Responsibility Principle violation
  • No dependency on the framework
  • Better performance

第一个优势可能出乎意料,因为来自 顶层组件比在字段中添加注释更难,并且 涉及更多样板代码.这肯定是一件坏事,对吧? 在这种情况下不行.实际上,与以下两个方面相关的好方面 这样的依赖关系传播.首先,依赖 将在顶层组件中显示.因此,只要看一下 在片段字段,代码阅读者将立即 了解此片段显示图像.这样的优化 易读性使系统更易于长期维护 学期.其次,使用案例的子类并不多 视图需要其他依赖项.您实际上需要 提供这些依赖关系的工作会给您一些 是时候考虑提供它们是否是一个好的设计决策了 首先.

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天全站免登陆