Dagger2活动范围,我需要多少个模块/组件? [英] Dagger2 activity scope, how many modules/components do i need?

查看:73
本文介绍了Dagger2活动范围,我需要多少个模块/组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于自定义范围,我有几个问题:


  1. 我正在使用MVP architecutre,我需要向不同的演示者注入不同的演示者活动。为此,我创建了@ActivityScope。

  2. 如果我仍然负责创建和释放这些依赖项,那么自定义范围批注的目的是什么?这是否意味着我必须为每个活动创建一个单独的模块/组件?不知道我是否正确,但是我可以在所有模块/组件中使用@ Scope123,并且不会有任何区别。


解决方案


这是否意味着我必须为每个活动创建一个单独的模块/组件?


是。



至少在 ,如果需要,您需要为每个活动创建一个新的组件 object 提供活动范围的依赖项,例如 Activity 本身, LoaderManager 或类似的东西,因为范围将一直存在只要活动是活动。



每个活动中是否需要模块和组件的问题在很大程度上取决于您的体系结构。另外,您也许可以制作一个通用的 ActivityModule ,提供您的模型,演示者并查看可以重用的模型。



您也可以只使用一个 Component 例如,如果仅需要活动的基本依赖项,例如 LoaderManager Activity 本身,则可以编写一个 ActivityModule 仅提供这些基础对象。然后,您可以将此模块与您的组件一起使用以提供依赖项。
如果您的Presenter(及其依赖项)可以通过构造函数注入创建,则可以使用单个组件和模块来完成所有活动。 p>

如果演示者和视图是已实现的接口,则需要创建一个提供实际实现的模块。


如果我仍然负责创建和释放依赖项,自定义范围注释的目的是什么?


范围用于使对这些依赖项的管理变得更容易。如前所述,活动范围因活动被销毁而死亡。通过具有这些作用域的依赖关系,您可以确保没有任何事情依赖于具有更高作用域/生存期并可能导致内存泄漏的活动。



此外,我喜欢思考可以将其作为一堆依赖项进行热交换,然后抛出。一个很好的例子是 @UserScope ,它将保存用户数据,他的登录信息,会话数据,...

如果我切换用户,我只需要用用户范围或更小的范围丢弃所有内容(关闭活动,删除UserComponent),并且与用户有关的所有内容都消失了。下一个可以登录,并且副作用低。



作用域主要是编译时检查,可以帮助您将层次结构引入依赖项 >,因为编译器所做的只是检查其中是否没有循环,并且没有任何东西请求它无法访问的作用域的依赖关系。


I have a couple of questions about custom scopes:

  1. I'm using MVP architecutre and I need to inject different presenters to different activities. For that purpose I've created @ActivityScope. Does it mean that I must create a separate module/component for every activitiy?
  2. What is the purpose of custom scope annotations if I'm still responsible for creating and releasing those dependencies? Not sure if I'm right but I can use @Scope123 in all my modules/components and it won't make any difference.

解决方案

Does it mean that I must create a separate module/component for every activitiy?

Yes. And no.

At the very least you need to create a new component object for each activity if you want to provide activity scoped dependencies, like the Activity itself, the LoaderManager, or similar things, because the scope will just live as long as the activity.

The question of whether you need a module and component for each and every single one of your activities depends strongly on your architecture. Also you might be able to make a generic ActivityModule providing your model, presenter, and view which you could reuse.

You could also be fine with just one Component e.g. if only basic dependencies of the activity are needed like the LoaderManager or the Activity itself then you can write one ActivityModule to just provide those base objects. You can then just use this module with your component to supply the dependencies. If your Presenter (and its dependencies) can be created by constructor injection you could be fine with a single component and module for all of your activities.

If your presenter and view are interfaces that get implemented you would need to create a module that provides the actual implementation, though.

What is the purpose of custom scope annotations if I'm still responsible for creating and releasing those dependencies?

Scopes are used to make managing of those dependencies easier. As mentioned, the activity scope dies with the activity being destroyed. By having those scoped dependencies you can be sure that nothing depends on your activity that has a higher scope / lifetime and could cause memory leaks.

Also, I like to think of it as bundles of dependencies you can hot swap and just 'throw out'. A good example is a @UserScope that would hold the user data, his login, session data, ...
If I switch users I just need to throw everything out with a user scope or less (close activity, remove UserComponent) and everything concerning the user is gone. The next one can login, and there is low risk of side effects.

Scopes are mostly compile time checks that help you bring hierarchy into your dependencies, since all the compiler does is check that there are no cycles in it and that nothing requests dependencies from a scope it can't access.

这篇关于Dagger2活动范围,我需要多少个模块/组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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