本文介绍了使用范围时依赖项注入的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我目前正在尝试找出Dagger2。我正在尝试设置4个范围:App,User,Activity和Fragment。用户和活动组件是应用程序的子组件。 Fragment是一个以Activity为依存关系的组件。

I'm currently trying to figure out Dagger 2. I am trying to set up 4 scopes: App, User, Activity, Fragment. User and Activity components are Subcomponents of App. Fragment is a Component with Activity as its dependency.

说我的UserSettingsActivity需要一个工具栏(由ActivityModule提供)和一个UserProfile(由UserModule提供)。在从数据库中请求UserProfile之前,我不会获得它,但是可以立即提供工具栏。因此,注入的顺序是先进入ActivityComponent,然后再进入UserComponent。我在活动中有2个@Inject字段,一个用于工具栏,一个用于UserProfile。我希望匕首会知道依赖关系来自不同的模块,但是似乎抱怨当将UserProfile注入ActivityComponent时无法提供。显然,ActivityModule无法提供它,但是为什么不建立由UserModule提供的UserProfile的连接呢?

Say my UserSettingsActivity needs a Toolbar (provided by ActivityModule), and a UserProfile (provided by UserModule). I won't get a UserProfile until I ask for it from the database, whereas the Toolbar can be provided right away. So the order of injection that takes place is into ActivityComponent first, then into UserComponent. I have 2 @Inject fields, one for Toolbar and one for UserProfile in the activity. I was hoping that dagger will know that the dependencies are coming from different modules, but it seems to complain that UserProfile can't be provided when injected into ActivityComponent. Obviously it can't be provided by ActivityModule, but why is it not making a connection that UserProfile is provided by UserModule?

推荐答案

据我所知,Dagger-2不支持部分注入。

To my best knowledge, Dagger-2 doesn't support "partial injections".

因此,当您调用 myComponent.inject(this),如果 myComponent 无法提供所有 @Inject 带注释的成员,则Dagger-2会引发错误 this

Therefore, when you call myComponent.inject(this), Dagger-2 throws an error if myComponent can't provide all @Inject annotated members of this.

我看到两种方法可以解决此限制:

I see two ways to work around this limitation:


  1. UserProfile 中删除​​ @Inject 批注,公开 UserProfile 通过 UserComponent 中的公共方法,并在 UserComponent 准备就绪时手动注入要使用的。类似于以下内容: userProfile = userComponent.getUserProfile()

  2. 不要创建 UserComponent 取决于数据获取。 UserComponent 可用于在以下位置注入工具栏和一些 UserProfileProvider 同时,您将从可用的 UserProfileProvider 中获取 UserProfile

  1. Remove @Inject annotation from UserProfile, expose UserProfile via public method in UserComponent and inject it manually when UserComponent is ready to be used. Something analogous to this: userProfile = userComponent.getUserProfile()
  2. Don't make UserComponent dependent on data fetching. UserComponent could be used to inject Toolbar and some UserProfileProvider at the same time, and you will fetch UserProfile from UserProfileProvider when it is available.

我个人认为第二种方法是更好的选择。应该使用DI库来满足构造时对象的依赖性。在Android中,我们无法自行构造 Activity Fragment ,因此我们在 onCreate中执行DI () onAttach() onCreateView()等,但确实如此并不意味着我们应该使用DI库来协助控制应用程序的流程。

I personally think that second approach is the better choice. DI libraries should be used in order to satisfy objects' dependencies at construction time. In Android we can't construct Activity or Fragment ourselves, therefore we perform DI in onCreate(), onAttach(), onCreateView(), etc., but it does not mean that we should be using DI libraries in order to assist in controlling the flow of applications.

这篇关于使用范围时依赖项注入的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 15:20