简体   繁体   English

Dagger组件依赖项的含义

[英]Dagger component dependency meaning

I'm experimenting with Dagger 2 and I'm just testing things out to understand the framework. 我正在尝试使用Dagger 2,并且只是在进行测试以了解框架。

I'm having a ApplicationComponent that needs to be a singleton for the whole app so I defined it like this: 我有一个ApplicationComponent需要整个应用程序一个单例,所以我这样定义它:

@Component(modules = {ApplicationModule.class})
@Singleton
public interface ApplicationComponent {
    Context provideContext();
}

With module: 带模块:

@Module
public class ApplicationModule {

    private Application appContext;

    public ApplicationModule(Application appContext) {
        this.appContext = appContext;
    }

    @Provides
    @Singleton
    public Context provideContext() {
        return appContext;
    }
}

Now I also want a NetworkComponent that needs to be a live as long as the app lives. 现在,我还需要一个只要应用程序有效就可以运行的NetworkComponent。 That network component needs to have a dependency on the ApplicationComponent. 该网络组件需要依赖于ApplicationComponent。 So I have my networkcomponent as follows: 因此,我的网络组件如下:

@Component(dependencies = {ApplicationComponent.class}, modules = {NetworkModule.class})
@PerApp
public interface NetworkComponent extends ApplicationComponent {
    @Named(DaggerConstants.DEFAULT_RETROFIT)
    Retrofit provideDefault();

    @Named(DaggerConstants.OTHER_RETROFIT)
    Retrofit provideOther();

    void inject(MainActivity activity);
}

Module: 模块:

@Module
public class NetworkModule {

    @Named(DaggerConstants.DEFAULT_RETROFIT)
    @PerApp
    @Provides
    Retrofit provideDefaultRetrofit() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.someurl.com/")
                .build();

        return retrofit;
    }

    @Named(DaggerConstants.OTHER_RETROFIT)
    @PerApp
    @Provides
    Retrofit provideOtherRetrofit() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.someotherurl.com/")
                .build();

        return retrofit;
    }

    @PerApp
    @Provides
    SharedPreferences networkPreferences(Context context) {
        return context.getSharedPreferences("network", Context.MODE_PRIVATE);
    }
}

I have some questions: 我有一些疑问:

1) I store the two components in the Application in Android. 1)我将两个组件存储在Android的应用程序中。 But to me it seems strange that I store the AppComponent and the NetworkComponent. 但是对我来说,存储AppComponent和NetworkComponent似乎很奇怪。 Is it not better that my ApplicationComponent should provide the NetworkComponent? 我的ApplicationComponent应该提供NetworkComponent更好吗?

2) The annotations @PerApp and stuff does it mean something or is Dagger just looking that there is an Object a live that has the @PerApp annotation and if not then it deletes that? 2)注释@PerApp和东西是什么意思,还是Dagger只是在看一个有@PerApp注释的对象,如果没有,它将删除它? This is not clear to me. 我不清楚。

3) Is it something useful to mark a Module with for example @Singleton because it is possible but I don't see that in any example. 3)用@Singleton标记模块是否有用,因为这是可能的,但在任何示例中我都看不到。

I store the two components in the Application in Android. 我将这两个组件存储在Android应用程序中。 But to me it seems strange that I store the AppComponent and the NetworkComponent. 但是对我来说,存储AppComponent和NetworkComponent似乎很奇怪。 Is it not better that my ApplicationComponent should provide the NetworkComponent? 我的ApplicationComponent应该提供NetworkComponent更好吗?

You are missing the concept of components. 您缺少组件的概念。 The idea behind the components is to have objects, whose lifetime differs from each other. 组件背后的想法是拥有对象,它们的寿命彼此不同。 For example: 例如:

  • Object A should be app singleton. 对象A应该是应用单例。 Whenever you need this object it would be the exact same object 每当您需要此对象时,它将是完全相同的对象
  • Object B should be activity singleton. 对象B应该是活动单例。 Each time your activity destroyed and created a new object would be created. 每次您的活动销毁并创建一个新对象时,都会创建一个。
  • Object C should be fragment singleton. 对象C应该是片段单例。 Each time your fragment is attached and detached to activity a new object would be created. 每次将片段附着和分离以进行活动时,都会创建一个新对象。

    So you specify, that you want ComponentC to have dependency of ComponentB , which has dependency on ComponentA (Assuming each of them provide the appropriate named dependency.) 因此,您指定希望ComponentC具有ComponentC依赖关系,而ComponentB依赖于ComponentA (假设它们每个都提供了适当的命名依赖关系。)

The annotations @PerApp and stuff does it mean something or is Dagger just looking that there is an Object a live that has the @PerApp annotation and if not then it deletes that? @PerApp注解和东西是什么意思,还是Dagger只是在看一个有@PerApp注解的对象,如果没有,它将删除它? This is not clear to me. 我不清楚。

Custom scopes are useful for the objects, who's lifetime you are taking responsibility for. 自定义范围对于对象(您要负责的生命周期)很有用。 Ie, if you have declared a component with your custom scope, you are responsible for clearing that component, thereafter next time that dependency is asked from component a new object would be created for you. 即,如果您已使用自定义范围声明了一个组件,则您有责任清除该组件,此后,下一次从组件中请求依赖项时,将为您创建一个新对象。

In the above example you have to take care of nulling out ComponentB as soon as your activity is being destroyed, otherwise it will provide you the same B object next time you ask for it. 在上面的示例中,您必须注意在活动被销毁后立即使ComponentB无效,否则它将在您下次请求时为您提供相同的B对象。

Is it something useful to mark a Module with for example @Singleton because it is possible but I don't see that in any example. 用例如@Singleton标记模块是否有用,因为有可能,但在任何示例中都看不到。

It would not make any difference concerning providing dependencies. 提供依赖关系不会有任何区别。 But maybe it will help you to remember what scope the hosting component has. 但是也许它可以帮助您记住托管组件的作用范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM