Android Dagger依赖周期 [英] Android dagger dependency cycle

查看:144
本文介绍了Android Dagger依赖周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个相互依赖且相互具有相同范围的依赖项。

I have 2 dependencies with the same Scope that require of each other.

我的依赖项是具有不同方法的域服务(每种方法都有不同的业务案例)。某些业务案例可能会使用其他域中的方法。

My dependencies are domain services with different methods ( each method a different business case). some business cases might use methods from another domain.

为此,我需要domain1可用于domain2,反之亦然。

In order to do this I need domain1 to be available for domain2 and vice versa.

但是当我这样做时,我得到了一个依赖循环编译错误。谷歌搜索一段时间后,我发现为了克服此问题,我必须使用@Inject批注而不是@Module中的构造函数参数来注入一个依赖项。

but when i do this i get a dependency cycle compilation error. after googling for some time i found that in order to overcome this issue i must inject one of the dependencies with the @Inject annotation instead of a constructor parameter in the @Module.

当我尝试这样做时,代码可以编译,但是dagger根本没有注入第二个依赖项。

when i try this the code compiles but dagger is not injecting the second dependency at all.

有没有办法用Dagger实现我想要的?

is there a way to achieve what i want with Dagger?

推荐答案

您的问题: AClass对BClass具有构造函数依赖性,而BClass对AClass具有构造函数依赖性。即使没有Dagger,这也行不通:如果它们相互依赖,那么您将首先创建哪个?

Your problem: AClass has a constructor dependency on BClass, which has a constructor dependency on AClass. Even without Dagger, this wouldn't work: If they depend on each other, which would you create first?

您尝试的解决方案:您可以使用 new 创建一个类(BClass),并且该类不再具有 @Inject 注释的构造函数,您可以等到构造AClass来填充BClass实例之后。但是,如果使用 new 创建对象,则需要通过将其传递到members-injection方法或 MembersInjector< BClass>中来注入对象。 对象。您还需要确保这在 @Provides 方法之外发生(因为 @Provides 的全部原因被调用,因此您可以构造一个值以传递到AClass的构造函数中。这是脆弱且相当丑陋的。

Your attempted solution: If you create one of your classes (BClass) using new, and it no longer has an @Inject-annotated constructor, you could wait until after AClass is constructed to populate your BClass instance. However, if you create an object using new, you'll need to inject it by passing it into a members-injection method or MembersInjector<BClass> object. You'll also need to make sure that this happens outside of the @Provides method (because the whole reason @Provides is being called is so you can construct a value to pass into AClass's constructor). This is fragile and fairly ugly.

我的建议:使用通过提供商进行间接。让AClass注入 Provider< BClass> 或BClass注入 Provider< AClass> 或两者。只要您不在构造函数中调用 get ,就可以允许Dagger创建AClass并将BClass的创建推迟到需要时再进行。您无需进行任何其他配置即可为任何T类注入 Provider< T> Lazy< T> 已绑定到您的组件中;请参阅《用户指南》中的图形中的绑定 有关可用注射剂的完整列表。

My suggestion: Use indirection via Provider. Have AClass inject Provider<BClass>, or BClass inject Provider<AClass>, or both. As long as you don't call get within the constructor, you'll allow Dagger to create AClass and defer the creation of BClass until you need it. You need no additional configuration in order to inject a Provider<T> or Lazy<T> for any class T you've bound in your Component; see "Bindings in the Graph" in the User's Guide for the full list of available injections.

这篇关于Android Dagger依赖周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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