Dagger2向组件添加范围标签的目的是什么? [英] Dagger2 what is the purpose of adding a scope tag to components?

查看:97
本文介绍了Dagger2向组件添加范围标签的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个组件,该组件仅在活动的整个生命周期内有效。我没有使用任何范围注释,而仅提供了组件寿命的简单示例:

I've created a component and it only last for the lifetime of the activity. I did not use any scope annotations and only quick example of the life time of the component looks like this:

public class MainActivity extends AppCompatActivity {

private final String TAG = getClass().getSimpleName();
@Inject
AlmondButter someAlmondButter;
@Inject
CashewSandwich sandwich;

SandwichComponent sandwichComponent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /*create thte dependent butter for the sandwich here*/
    ButterComponent butterComponent=DaggerButterComponent.builder().
            butterModule(new ButterModule()).build();
    /*create a scope sandwichcomponent here */

    sandwichComponent=DaggerSandwichComponent.builder().sandwichModule(new SandwichModule()).
            butterComponent(butterComponent)
            .build();
    //finally we have a sandwichComponent, lets inject our dependencies
    sandwichComponent.inject(this);

    Log.v(TAG,sandwich.toString());
    Log.v(TAG,someAlmondButter.toString());
}

@Override
protected void onDestroy() {
    super.onDestroy();
    //not necessary but it clearly shows the scope being tied to lifecycle of activity
    sandwichComponent=null;
}

}

我的所有组件都没有注释范围,并且工作正常。所以我很困惑为什么有些人建议创建范围标签,目的是什么?我将在下面向您展示我的组件以供参考:

none of my components are scoped with anotations and it works fine. So im confused why some recommend to create scope tags, what is there purpose ? i'll show you my components below for reference:

    @Component(dependencies = ButterComponent.class, modules = SandwichModule.class)
public interface SandwichComponent {
    CashewSandwich ProvideCashewSandwitch();
    void inject (MainActivity mainactivity);
}

和下一个组件:

    @Component(modules={ButterModule.class})

public interface ButterComponent {
     //these are for our whatever class depends on butter
     AlmondButter ProvideAlmondButter();
     CashewButter ProvideCashewButter();
}

更新:对于需要了解这些概念的任何人,我在博客上发表的评论< a href = http://j2emanue.blogspot.com/ rel = nofollow>这里。

UPDATE: FOR ANYONE WHO NEEDS HELP WITH UNDERSTANDING THESE CONCEPTS I MADE A BLOG HERE.

推荐答案

通过使用组件作用域和模块提供程序作用域,您可以要求Dagger2为您创建作用域提供程序

By using scopes on component and scopes on module provider method, you can ask Dagger2 to create scoped providers for you.

要在模块的provider方法中获取有作用域的提供程序,还必须将作用域放在组件上。

In order to get a scoped provider in your module's provider method, you must put the scope on the component as well.

您只能在给定组件上指定一个作用域,而在有作用域的组件中,您只能在其提供程序方法上具有具有该范围的模块,或者提供程序方法可以

You can specify only one scope on a given component, and in a scoped component, you can only have modules with that scope on its provider methods, or the provider methods can also be unscoped.

未限制范围的提供程序会在每次注入调用时为您提供一个新实例。
范围限定的提供程序为对该特定组件实例的每次注入调用存储一个实例。

Unscoped providers give you a new instance on every inject call. Scoped providers store a single instance for every inject call to that specific component instance.

@Component(modules={HelloModule.class})
@Singleton
public interface HelloComponent {
    Hello hello();
    World world();

    void inject(MainActivity mainActivity);
}

@Module
public class HelloModule {
    @Provides
    public Hello hello() { return new Hello(); } //new instance each call to inject

    @Provides
    @Singleton
    public World world() { return new World(); } //one instance per component
}

另一个组件继承其依赖项(使用子组件或组件依赖项),则只能依赖另一个作用域组件。就像在Java中不允许多重继承一样,您也不能依赖多个作用域组件并继承它们的依赖关系,只有一个。

It is also worth noting that if you subscope another component to inherit its dependencies (using subcomponent or component dependency), you can only depend on one other scoped component. Think of it like how "multiple inheritance" is not allowed in Java, you also cannot depend on multiple scoped components and inherit their dependencies, only one.

通常,您有一个单例作用域,然后根据应用程序中模块的顶级分离对组件进行细分。

Typically you have a singleton scope, and you subscope your components according to the top-level separation of modules in your application.

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

@Component(dependencies={ApplicationComponent.class}, modules={MainActivityModule.class}) 
@ActivityScope 
//this is a subscoped component that inherits from ApplicationComponent
public interface MainActivityComponent extends ApplicationComponent {
    OtherThing otherThing();

    void inject(MainActivity mainActivity);
}

根据Martin Fowler的说法,将应用程序切成顶层的正确方法是使用功能,例如 GalleryComponent SettingsComponent 等,而不是按层(数据,域,表示形式)进行。

According to Martin Fowler, the right way to slice your application into pieces on the top-level is by features, such as GalleryComponent, SettingsComponent, etc. and not by layers (data, domain, presentation).

这篇关于Dagger2向组件添加范围标签的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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