Dagger组件依赖项的含义 [英] Dagger component dependency meaning

查看:82
本文介绍了Dagger组件依赖项的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验Dagger 2,我只是在测试一些东西以了解框架。

I'm experimenting with Dagger 2 and I'm just testing things out to understand the framework.

我有一个ApplicationComponent,该组件必须是单例对于整个应用程序,因此我将其定义为:

I'm having a ApplicationComponent that needs to be a singleton for the whole app so I defined it like this:

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

使用模块:

@Module
public class ApplicationModule {

    private Application appContext;

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

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

现在,我也想要一个需要成为只要应用程序有效就可以生存。
该网络组件需要依赖于ApplicationComponent。
因此,我的网络组件如下:

Now I also want a NetworkComponent that needs to be a live as long as the app lives. That network component needs to have a dependency on the 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
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);
    }
}

我有一些问题:

1)我将两个组件存储在Android的Application中。
但是对我来说,我存储AppComponent和NetworkComponent似乎很奇怪。
我的ApplicationComponent应该提供NetworkComponent更好吗?

1) I store the two components in the Application in Android. But to me it seems strange that I store the AppComponent and the NetworkComponent. Is it not better that my ApplicationComponent should provide the NetworkComponent?

2)注释@PerApp和其他东西意味着什么,或者Dagger只是看那里是具有@PerApp批注的实时对象,如果没有,则将其删除?

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? This is not clear to me.

3)用@Singleton标记模块是否有用,因为这是可能的,但我看不到

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.

推荐答案


我将这两个组件存储在Android应用程序中。但是对我来说,存储AppComponent和NetworkComponent似乎很奇怪。我的ApplicationComponent应该提供NetworkComponent更好吗?

I store the two components in the Application in Android. But to me it seems strange that I store the AppComponent and the NetworkComponent. Is it not better that my ApplicationComponent should provide the 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:


  • 对象 A 应该是应用单例。每当需要此对象时,它就是完全相同的对象

  • 对象 B 应该是活动单例。每次您的活动销毁并创建一个新对象时,都会创建该对象。

  • 对象 C 应该是片段单例。每次将片段附着和分离到活动中时,都会创建一个新对象。

  • Object A should be app singleton. Whenever you need this object it would be the exact same object
  • Object B should be activity singleton. Each time your activity destroyed and created a new object would be created.
  • Object C should be fragment singleton. Each time your fragment is attached and detached to activity a new object would be created.

因此,您指定要使用 ComponentC 具有 ComponentB 的依赖关系,该依赖关系与 ComponentA 的依赖关系(假定它们每个都提供了适当的命名依赖关系) )

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.)


注释 @PerApp ,这意味着什么?或者Dagger只是在看一个具有@PerApp批注的Live对象,如果没有,它将删除它?这对我来说还不清楚。

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? This is not clear to me.

自定义作用域对于对象很有用,您将终生负责。即,如果您已声明一个具有自定义范围的组件,则您有责任清除该组件,此后,下一次从该组件中请求依赖项时,将为您创建一个新对象。

Custom scopes are useful for the objects, who's lifetime you are taking responsibility for. I.e., 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.

在上面的示例中,一旦活动被销毁,您就必须使 ComponentB 无效,否则它将为您提供相同的<$ c $

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.


将c> B 对象用作下一个对象是不是有用呢?例如@Singleton,因为有可能,但在任何示例中我都看不到。

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.

在提供依赖项方面没有任何区别。但是也许它可以帮助您记住托管组件的作用范围。

It would not make any difference concerning providing dependencies. But maybe it will help you to remember what scope the hosting component has.

这篇关于Dagger组件依赖项的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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