简体   繁体   English

使用Dagger 2的活动场注入

[英]Activity Field Injection Using Dagger 2

When using Constructor Injection with Dagger2, I can really see how the concept of dependency injection is implemented: 当使用带有Dagger2的Constructor Injection时,我真的可以看到依赖注入的概念是如何实现的:

 public class Dependent {

     @Inject
     public Dependent(Dependency dependency) {
         // We're dependent on an instance of the Dependency class
         // but we don't care who provides it
     }

 }

But when it comes to an Activity , because Android is instantiating it for us, we need to use Field Injection to satisfy our dependencies. 但是当涉及到Activity ,因为Android正在为我们实例化它,我们需要使用Field Injection来满足我们的依赖关系。 All of the examples I found online suggest something like this: 我在网上找到的所有例子都是这样的:

  1. Create a @Module class to provide our dependency. 创建一个@Module类来提供我们的依赖项。
  2. Create a @Component interface and call the generate builder to instantiate it. 创建一个@Component接口并调用generate构建器来实例化它。 Most of the examples perform this in the Application class and save a reference as a member. 大多数示例在Application类中执行此操作并将引用保存为成员。
  3. In our Activity - create our @Inject field, and in the onCreate(..) method, get a reference to the Component from our Application and initiate the injection. 在我们的Activity - 创建我们的@Inject字段,并在onCreate(..)方法中,从我们的Application获取对Component的引用并启动注入。

My problem with this approach is that it doesn't feel loosely coupled, which is what we're trying to achieve. 我对这种做法的问题是,它不觉得松散耦合,这是我们正在努力实现的。

 public class DependentActivity extends Activity {

     @Inject Dependency mDependency;

     public void onCreate(final Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         ((MyApplication) getApplication()).getComponent().inject(this);

         // We're dependent on an instance of dependency
         // but now we not only know who provides it,
         // we explicitly initiate the injection
     }

 }

What am I missing? 我错过了什么?

You initiate the injection, yes, but you don't have to have any knowledge about where it's coming from (meaning how the object is created, or how you get it). 你启动注射,是的,但你不必知道它来自何处(意味着如何创建对象,或者你如何获得它)。 You could switch out your component classes that your Application gives you and your activity wouldn't change. 您可以切换应用程序为您提供的组件类,并且您的活动不会更改。 You could switch out the module that provides your Dependent object and nothing in your activity would change. 您可以切换提供Dependent对象的模块,并且您的活动中的任何内容都不会更改。 What this injection gives you is the ability to have your activity only deal with USING the injected object. 此注入为您提供的是让您的活动仅处理使用注入对象的能力。 Usually without dependency injection your activity would have to understand how to instantiate it, how to initialize it with whatever it needs, and also how to use it. 通常没有依赖注入,您的活动必须了解如何实例化它,如何使用它需要的任何东西来初始化它,以及如何使用它。 This allows you to easily swap in and out components, change the way it's initialized, or whatever and your activity (ideally) wouldn't have to change a single thing. 这使您可以轻松地交换进出组件,更改其初始化方式,或者您的活动(理想情况下)不必更改单个内容。

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

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