本文介绍了无法理解匕首依赖注入概念-Android上的Dagger 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解dagger2并在我的应用中实现。我已经读了很多关于它的好处。除非并且直到我完全理解它,否则我无法在应用程序中获得它的好处。

I am trying to understand dagger2 and implement in my app. I have read a lot about its benefits. Unless and until I understand it completely, I cannot get the benefits of it in my app.

我了解@Module和@Inject。令我困惑的是@Component。我对此几乎没有疑问。

I have understood @Module and @Inject. The one that confuses me is @Component. I have few questions related to that.


  1. 模块提供对象实例并由Inject使用它。为什么我们需要介于两者之间的组件?真的有必要缩小差距吗?我们可以有没有任何方法的空接口组件吗?

  1. Module provides object instances and Inject uses it. Why do we need component in between? Is it really necessary to bridge the gap? Can we have empty Interface components without any methods?

模块类真的需要构造函数吗?如果模块类中没有构造函数,我们可以使用空构造函数初始化模块类吗?

Is constructor really necessary for module class? If there is no constructor in module class, Can we initialize module class using empty constructor?

为什么我们不能直接实例化模块类并构建依赖关系图

Why can't we directly instantiate module class and build dependency graph instead of creating component and then initializing it?

到目前为止,我在组件接口中只看到两种方法

I have seen only two kinds of methods in component interface so far

a。无效注入(活动/服务/片段); -为什么我们需要为此方法提供活动或服务的实例或片段?为什么我们不能有这样的东西-

a. void inject(Activity/Service/Fragment); - Why do we need to provide an instance of activity or service or fragment to this method? why can't we have something like this -

void inject(); -组件是否还会生成依赖关系图?

void inject(); - Will the Component still generate dependency graph?

我们可以从活动或服务以外的其他类中注入吗?还是可以分段这样的东西?-

Can we inject from some other class other than activity or service or fragment something like this -

void inject(DataManager dataManager);

void inject(DataManager dataManager);

如果DataManager是单例实例怎么办?

What If DataManager was a singleton instance?

b。改造getRetrofit();此方法与上述方法有什么区别?为什么它不带任何输入参数?

b. Retrofit getRetrofit(); What is the difference between this method and above method? Why does not this take any input parameters?

我读到@Singleton只是匕首中的作用域。

I read that @Singleton is just a scope in dagger. How can we actually create a singleton object that lives for lifetime of the application?

让我们假设实际上有一个我想使用匕首构建的DataManager实例,我们该如何创建一个在应用程序的整个生命周期中都可以生存的对象。它只有一个依赖性。我为此编写了一个模块类和一个组件接口。如果要在MainActivity中使用它,我将其用作

Let's suppose there is a DataManager instance that I want to build using dagger. It is having only one dependency. I wrote a module class and a component interface for that. If I want to use this in let's say MainActivity, i use it as

@Inject DataManager dataManager;

@Inject DataManager dataManager;

...

@Override

@Override

受保护的无效onCreate(Bundle savedInstanceState){

protected void onCreate(Bundle savedInstanceState) {

DataManagerComponent.Builder()。DataManagerModule(new DataManagerModule())。build();

DataManagerComponent.Builder().DataManagerModule(new DataManagerModule()).build();

}

我想在其他许多活动中使用此数据管理器,但我不希望它成为
单身人士。我想将其保留在使用它的当前活动范围内。因此,我
将使用

I want to use this datamanager in many other activities and I don't want it to besingleton. I want to keep it to the current activity scope where I use it. So Iwill use

@Inject DataManager dataManager;

@Inject DataManager dataManager;

来获取该实例。我应该

to get that instance. Should Iwrite

 

You need to mention the target in Dagger. It can't be interface, Object can be singleton it doesn't matter.

4 b-> Retrofit getRetrofit(); 此方法与以上方法有什么区别?为什么它不带任何输入参数?
这是组件的定义。如果您使用构造函数注入,并说如果您更改输入参数的数量,则不必在每个位置更改代码。 Dagger 会照顾好它。
以上代码我相信是组件定义的一部分,因此当您进行组件依赖时,它将帮助您将其公开给外部图形

4 b -> Retrofit getRetrofit(); What is the difference between this method and above method? Why does not this take any input parameters? This is definition of component. If you use constructor injection and let says if you change the number of input parameters you wouldn't have to go and change the code every place. Dagger will take care of it. Above code I believe is part of component definition so when you do component dependencies it will help you to expose it to outer graph

5-> 我读到 @Singleton 只是 dagger 中的作用域。我们究竟如何才能创建一个在应用程序生命周期内有效的 singleton对象?
如果您指定范围和只需创建一次该组件,然后就可以使用一生。通常,具有应用程序范围组件的应用程序是在APP的Application类中创建的。

5 -> I read that @Singleton is just a scope in dagger. How can we actually create a singleton object that lives for lifetime of the application? The Thumb rule is in you module if you give a scope and create that component just once then It would be there for the life time. Generally one with application scope components are created in the Application class of your APP.

6-> *让我们假设存在一个<$ c我想使用匕首构建的$ c> DataManager 实例。它只有一个依赖性。我为此写了一个模块类和一个组件 interface 。如果要在 MainActivity 中使用它,我将其用作

6 -> *Let's suppose there is a DataManager instance that I want to build using dagger. It is having only one dependency. I wrote a module class and a component interface for that. If I want to use this in let's say MainActivity, I use it as

@Inject DataManager dataManager;

...

@Override

protected void onCreate(Bundle savedInstanceState) {

DataManagerComponent.Builder().DataManagerModule(new DataManagerModule()).build();

}

我要使用此 datamanager 在其他许多活动中,我不希望它是单例的。我想将其保留在使用它的当前活动范围内。因此,我将使用

I want to use this datamanager in many other activities and I don't want it to be singleton. I want to keep it to the current activity scope where I use it. So I will use

@Inject DataManager dataManager;

来获取该实例。我应该写

to get that instance. Should I write

DataManagerComponent.Builder...........

每个活动 oncreate()我在其中使用 @Inject DataManager dataManager ?如果我要写的话,它会不会创建比仅仅使用

in each and every activity oncreate() where I use @Inject DataManager dataManager? If I have to write that, Will it not create more boilerplate code than simply using

DataManager dataManager = new DataManager();*

在这里,您不应给 datamanager 提供程序,然后每次您说 @inject datamanager 时,它都会作为不同的对象实例出现,即使您的组件是 singleton 范围。

Here You should not give any scope to the datamanager provider and then every time u say @inject datamanager it would come as different object instance even if your component is singleton scope.

7-> *让我们假设有4个对象,它们彼此依赖,例如D依赖C,C依赖

7 -> *Let's suppose there are 4 objects and they are dependent on each other like D dependent on C, C dependent on B etc.

D -> C -> B -> A

假设我已经编写了模块类并为所有4提供了方法。如果我尝试注入D在任何 ActivityA 中,例如

Let's suppose I have written module class and provides method for all 4. If I try to inject D in any ActivityA like

@Inject D d;

C,B,A是否会自动实例化?

Will C, B, A instantiated automatically?

假设在 ActivityB 中,我只需要注入B。如果我像这样注入B,

Let's suppose in ActivityB I just need to inject B. If I inject B like

@Inject B b;

匕首会再次创建B和A吗?还是使用已经创建的对象?*
如果通过构造函数进行注入,或者必须使用成员注入器,则会创建该对象。

Will dagger create B and A again? Or will it use the ones which are already created ?*It will be create if the injection is via constructor or else you've to use members injector.

请浏览 YouTube教程中有关 dagger2 的扭曲方程式。这帮助我理解了 Dagger2 。

Please go over YouTube tutorials of twisted equations for dagger2. That has helped me understand Dagger2.

如果您有帮助,请投票。

Do vote if you this helps you.

这篇关于无法理解匕首依赖注入概念-Android上的Dagger 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 20:03