dra*_*siv 5 mvp android dagger dagger-2
我已经使用 Dagger 2 一段时间了,但我仍在尝试解决一些问题。我仍然管理得不好的一件事是为不同情况设置模块和组件,例如具有多个片段的活动。我见过很多实现,大多数时候都有点不同。
那么,让我公开一下我当前使用 MVP 的应用程序结构,如果我的实现是否正确,我想听听一些意见。
@Module
public final class ApplicationModule {
private Context mContext;
public ApplicationModule(Context context){
mContext = context;
}
public ApplicationModule(){
mContext = null;
}
@Provides
Context provideContext(){
return mContext;
}
@Singleton
@Provides
public SharedPreferences getAppPreferences(){
return mContext.getSharedPreferences("CalorieApp",Context.MODE_PRIVATE);
}
}
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(MainApplication mainApplication);
SharedPreferences sharedPreferences();
}
Run Code Online (Sandbox Code Playgroud)
在此 AppModule 中,我通常只会设置我的应用程序需要的 Singleton。就像 SharedPreferences 或与网络请求相关的任何内容。这个模块和组件在某种程度上是标准的,我总是通过这样创建它们来启动我的应用程序。
然后我为 Activity 设置模块和组件,它将依赖于 ApplicationComponent
@Module
public class ActivityModule {
private Activity activity;
public ActivityModule(Activity activity){
this.activity = activity;
}
@Provides
Activity provideActivity(){
return activity;
}
}
@PerActivity
@Component(dependencies = ApplicationComponent.class, modules =
ActivityModule.class)
public interface ActivityComponent {
void inject(WelcomeActivity welcomeActivity);
void inject(MainActivity mainActivity);
}
Run Code Online (Sandbox Code Playgroud)
现在,MainActivity 有 3 个片段,我将为片段创建 3 个模块和 1 个组件
@Module
public class HomeFragmentModule {
private HomeFragmentContract.View mView;
public HomeFragmentModule(HomeFragmentContract.View view){
mView = view;
}
@Provides
HomeFragmentContract.View provideHomeFragmentView(){
return mView;
}
}
@Module
public class ChartsFragmentModule {
private ChartsFragmentContract.View mView;
public ChartsFragmentModule(ChartsFragmentContract.View view){
mView = view;
}
@Provides
ChartsFragmentContract.View provideChartsFragmentView(){
return mView;
}
}
@Module
public class ProfileFragmentModule {
private ProfileFragmentContract.View mView;
public ProfileFragmentModule(ProfileFragmentContract.View view){
mView = view;
}
@Provides
ProfileFragmentContract.View provideProfileFragmentContract(){
return mView;
}
}
@PerFragment
@Component(dependencies = ActivityComponent.class ,
modules = {ChartsFragmentModule.class, HomeFragmentModule.class,
ProfileFragmentModule.class})
public interface FragmentComponent {
void inject(ChartsFragment chartsFragment);
void inject(HomeFragment homeFragment);
void inject(ProfileFragment profileFragment);
}
Run Code Online (Sandbox Code Playgroud)
然后我必须实例化 Dagger,首先在我的应用程序类中,然后在每个活动和片段中
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
Run Code Online (Sandbox Code Playgroud)
例如在WelcomeActivity中,我像这样实例化它:
DaggerActivityComponent.builder()
.activityModule(new ActivityModule(this))
.applicationComponent(((MainApplication)
getApplication()).getApplicationComponent())
.build()
.inject(this);
Run Code Online (Sandbox Code Playgroud)
在 MainActivity 中,我执行与上面相同的操作,但我正在为其中的活动组件创建一个 getter。
然后在每个片段中我都像这样实例化:
DaggerFragmentComponent.builder()
.homeFragmentModule(new HomeFragmentModule(this))
.activityComponent(((MainActivity)getActivity()).getActivityComponent())
.build()
.inject(this);
Run Code Online (Sandbox Code Playgroud)
此时一切正常。我可以注入 Presenters 和任何我想要的东西,但我不确定这是否是正确的方法。
您觉得我的实现怎么样?
另外,我还有一个 Repository 类,将在每个 Presenter 中使用,以将信息从 Firebase 显示到 UI 。
您会为此创建一个组件和模块,然后使所有片段都依赖于它吗?
希望我没有问太多问题,但我真的很想清理我的想法。
谢谢
你的设置非常好。绝对是我见过的最好的之一。
我想建议一些小的改变,从长远来看,这些改变会让你的生活更轻松。
子组件而不是组件依赖项:
我还没有看到指定组件之间的依赖关系比使用子组件更好的用例。
子组件可以直接访问父组件的整个对象图。这样做有两个优点:
在这种情况下,约定优于配置的一般原则适用于有利于子组件的情况。
无需区分 Activity 和 Fragments:
我注意到活动所需的对象图通常与片段所需的对象图非常相似。
在我的 MVC/MVP 方法中,我将“活动”和“片段”指定为控制器,并使用一个 ControllerComponent 将依赖项注入到控制器中。
然而,即使使用另一种 MVC/MVP 方法(或根本不使用),如果您从概念上考虑它们 - 活动和片段具有非常相似的功能。
因此,我建议使用一个组件来注入活动和片段。
每个片段不需要一个模块:
我已经在这里回答了关于每个 Activity/Fragment 有一个组件/模块的问题。请阅读该答案 - 它还提供了与此相关的代码示例以及上述所有建议。
恕我直言,模块应该按问题域而不是使用它们的组件对依赖项进行分组。例如,电子商务应用程序可能具有以下模块:NetworkingModule、CurrencyModule、CartModule、CheckoutModule等...
实际实现示例:
我前段时间开源了自己的应用程序,恕我直言,它具有相对较好的依赖注入结构。您可以在此处查看此结构。
我对这个 DI 结构还不满意的一个方面是,并非所有依赖项都按域分布在模块中。你可以做得更好。
订阅我的博客:
我觉得在这里插入这个有点不舒服,但我们现在正在录制有关 Dagger 的高级视频教程。过去一个月我们一直在制作本教程,预计会在 2-3 周内准备就绪。
本教程将准确讨论您所要求的内容 - 如何构建 Dagger 代码以实现可维护性。您可以在www.techyourchance.com上订阅我的博客,以便在发布时收到通知。
希望这有帮助。
| 归档时间: |
|
| 查看次数: |
1149 次 |
| 最近记录: |