在 Presenter class 中实例化 DI 组件是一种好习惯吗?
Is it good practice to instantiate DI component in presenter class?
我最近开始学习Dagger2。为此,我编写了一个应用程序。
应用架构:
ComicListFragment.java是主要片段
这个片段有一个演示器,它将所有与逻辑相关的东西保存在一个名为 ComicListFragmentPresenter.java[=12= 的演示器 class 中]
ComicListFragmentPresenter.java 初始化 Dagger2 DI 组件以注入所需的字段。
ComicListFragment.java调用ComicListFragmentPresenter.javapublic方法使使用依赖项和调用网络调用等
问题:
- 在 Presenter class 中实例化片段的依赖项而不是在 Fragment 中实例化 DI 组件并通过 Presenter class 构造函数注入依赖项 & 稍后使用访问这些依赖项是一种好习惯吗片段中的吸气剂 class?
请提出建设性意见。
Is it a good practice to instantiate a fragment's dependencies in its Presenter class instead of instantiating the DI component in Fragment and injecting the dependencies through Presenter class constructor & later on accessing those dependencies using getters inside the Fragment class?
没有。正如来自 Mark Keen, Fragments, Activities, and Services are chosen as injection targets as they are instantiated by the Android OS and we do not have control over the constructor. The other classes you make should prefer constructor injection for testability so you can swap in test doubles. Furthermore, the new dagger.android
的评论所述,库设置为易于使用 Fragments 和 Activity 进行注入,因此如果您明确请求从 Presenter 或任何其他 POJO 中的组件注入,您将失去这种好处。
这是您当前的演示者:
@Inject
ComicsCacheDBController mComicsCacheDBController;
@Inject
Retrofit mRetrofit;
@Inject
Picasso mPicasso;
/**
* Instantiate {@link ComicListFragmentPresenter}
* @param fragment {@link ComicListFragment} instance
*/
public ComicListFragmentPresenter (ComicListFragment fragment)
一个更可测试的演示者将只在构造函数中具有这些依赖项(并且可能依赖于不太通用的依赖项):
@Inject //this is important
public ComicListFragmentPresenter (ComicListFragment fragment, ComicsCacheDBController comicsCacheDBController, Retrofit retrofit, Picasso picasso)
注意构造函数上的 @Inject
注释。它请求 Dagger 2 注入所有这些依赖项。如果您已正确配置它,那么当您在 Fragment 中进行字段注入时:
@Inject ComicsListPresenter comicsListPresenter;
ComicListPresenter 的整个对象图将同时注入。
If I have 100 fragments, theoretically I will have 200 additional files just to drive that fragment which can very easily get out of control
Dagger 2 和其他此类依赖注入框架可帮助您解决特定问题;它们允许您在构造函数中传递可以交换测试替身的依赖项,从而使编写可测试的 类 变得容易。为此,您付出的代价是必须编写轻量级的样板文件 类。它们真的很适合 "Uncle Bob" programming style (small classes in lots of little files, strong encapsulation). If you'd rather not have so many small classes and would prefer to group more into one file (some people do) 然后你就不必使用它了。
如果您学习 IDE 快捷方式,您可以非常快速地编写样板文件。编写字段然后按 Cmd+N 或 Alt+Ins 让 Android Studio 为您生成构造函数。
我最近开始学习Dagger2。为此,我编写了一个应用程序。
应用架构:
ComicListFragment.java是主要片段
这个片段有一个演示器,它将所有与逻辑相关的东西保存在一个名为 ComicListFragmentPresenter.java[=12= 的演示器 class 中]
ComicListFragmentPresenter.java 初始化 Dagger2 DI 组件以注入所需的字段。
ComicListFragment.java调用ComicListFragmentPresenter.javapublic方法使使用依赖项和调用网络调用等
问题:
- 在 Presenter class 中实例化片段的依赖项而不是在 Fragment 中实例化 DI 组件并通过 Presenter class 构造函数注入依赖项 & 稍后使用访问这些依赖项是一种好习惯吗片段中的吸气剂 class?
请提出建设性意见。
Is it a good practice to instantiate a fragment's dependencies in its Presenter class instead of instantiating the DI component in Fragment and injecting the dependencies through Presenter class constructor & later on accessing those dependencies using getters inside the Fragment class?
没有。正如来自 Mark Keen, Fragments, Activities, and Services are chosen as injection targets as they are instantiated by the Android OS and we do not have control over the constructor. The other classes you make should prefer constructor injection for testability so you can swap in test doubles. Furthermore, the new dagger.android
的评论所述,库设置为易于使用 Fragments 和 Activity 进行注入,因此如果您明确请求从 Presenter 或任何其他 POJO 中的组件注入,您将失去这种好处。
这是您当前的演示者:
@Inject
ComicsCacheDBController mComicsCacheDBController;
@Inject
Retrofit mRetrofit;
@Inject
Picasso mPicasso;
/**
* Instantiate {@link ComicListFragmentPresenter}
* @param fragment {@link ComicListFragment} instance
*/
public ComicListFragmentPresenter (ComicListFragment fragment)
一个更可测试的演示者将只在构造函数中具有这些依赖项(并且可能依赖于不太通用的依赖项):
@Inject //this is important
public ComicListFragmentPresenter (ComicListFragment fragment, ComicsCacheDBController comicsCacheDBController, Retrofit retrofit, Picasso picasso)
注意构造函数上的 @Inject
注释。它请求 Dagger 2 注入所有这些依赖项。如果您已正确配置它,那么当您在 Fragment 中进行字段注入时:
@Inject ComicsListPresenter comicsListPresenter;
ComicListPresenter 的整个对象图将同时注入。
If I have 100 fragments, theoretically I will have 200 additional files just to drive that fragment which can very easily get out of control
Dagger 2 和其他此类依赖注入框架可帮助您解决特定问题;它们允许您在构造函数中传递可以交换测试替身的依赖项,从而使编写可测试的 类 变得容易。为此,您付出的代价是必须编写轻量级的样板文件 类。它们真的很适合 "Uncle Bob" programming style (small classes in lots of little files, strong encapsulation). If you'd rather not have so many small classes and would prefer to group more into one file (some people do) 然后你就不必使用它了。
如果您学习 IDE 快捷方式,您可以非常快速地编写样板文件。编写字段然后按 Cmd+N 或 Alt+Ins 让 Android Studio 为您生成构造函数。