无法在Android的架构样品待办事项-MVP匕首

问题描述:

我想通过这个Android的架构示例代码来了解组件(TasksRepositoryComponent): - https://github.com/googlesamples/android-architecture/tree/todo-mvp-dagger/todoapp无法在Android的架构样品待办事项-MVP匕首

我已阅读本用户指南以及 - https://google.github.io/dagger/users-guide

这是代码TasksRepositoryComponent

@Singleton 
@Component(modules = {TasksRepositoryModule.class, ApplicationModule.class}) 
public interface TasksRepositoryComponent { 

    TasksRepository getTasksRepository(); 
} 

ToDoApplication WHI CH延伸应用具有的onCreate()这个代码 -

mRepositoryComponent = DaggerTasksRepositoryComponent.builder() 
       .applicationModule(new ApplicationModule((getApplicationContext()))) 
       .build(); 

我不明白几件事情: -

  1. 为什么不TasksRepositoryComponent有像其他组件的注入()方法应用程序?

  2. 为什么ToDoApplication在build()之前也调用DaggerTasksRepositoryComponent中的tasksRepositoryModule()方法?为什么不需要?

有什么地方可以找到使用dagger2时要遵守的规则的好文档吗?

+1

这可能帮助:http://stackoverflow.com/questions/40562481/dagger-2-lack-of-constructor-injection-示例/ 40563125#40563125和http://stackoverflow.com/questions/40545075/dagger2-and-android/40546157#40546157虽然还有很多东西我还没有使用('@ IntoSet','@ Binds',等等。) – EpicPandaForce

为什么不TasksRepositoryComponentinject()方法像在应用 其他组件?

TaskRepositoryComponent是一个父组件,其唯一目的是将TaskRepository的绑定发布到它的依赖组件。依赖组件将继承TaskRepository的绑定并能够将其注入其注入位置。有关此功能的更多信息,请参阅documentation for dependent components

为什么不ToDoApplicationbuild()之前调用DaggerTasksRepositoryComponent方法 tasksRepositoryModule()?为什么不需要?

TasksRepositoryModule有一个没有参数的默认公共构造函数,所以生成的组件可以初始化它,而不必在构建器中显式构造它。看一看生成的代码中DaggerTasksRepositoryComponent - 你会看到类似

if (tasksRepositoryModule == null) { 
    tasksRepositoryModule = new TasksRepositoryModule(); 
}