Dagger 2 组件中的 getter 方法的目的是什么? [英] What is the purpose of the getter methods in Components in Dagger 2?

查看:29
本文介绍了Dagger 2 组件中的 getter 方法的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解 Dagger 2 中的组件.这是一个例子:

I am trying to understand Components in Dagger 2. Here is an example:

@Component(modules = { MyModule.class })
public interface MyComponent {

    void inject(InjectionSite injectionSite);

    Foo foo();

    Bar bar();   

}

我了解 void inject() 方法的作用.但我不明白其他 Foo foo() getter 方法是做什么的.这些其他方法的目的是什么?

I understand what the void inject() methods do. But I don't understand what the other Foo foo() getter methods do. What is the purpose of these other methods?

推荐答案

Dagger 是一种连接对象及其依赖关系图的方法.作为直接调用构造函数的替代方法,您可以通过从 Dagger 请求实例来获取实例,或者提供一个您希望注入 Dagger 创建的实例的对象.

Dagger is a way of wiring up graphs of objects and their dependencies. As an alternative to calling constructors directly, you obtain instances by requesting them from Dagger, or by supplying an object that you'd like to have injected with Dagger-created instances.

让我们开一家咖啡店,这取决于 Provider 和 CashRegister.假设您已将它们连接到一个模块中(可能是 LightRoastCoffee 和 DefaultCashRegister 实现).

Let's make a coffee shop, that depends on a Provider<Coffee> and a CashRegister. Assume that you have those wired up within a module (maybe to LightRoastCoffee and DefaultCashRegister implementations).

public class CoffeeShop {
  private final Provider<Coffee> coffeeProvider;
  private final CashRegister register;

  @Inject
  public CoffeeShop(Provider<Coffee> coffeeProvider, CashRegister register) {
    this.coffeeProvider = coffeeProvider;
    this.register = register;
  }

  public void serve(Person person) {
    cashRegister.takeMoneyFrom(person);
    person.accept(coffeeProvider.get());
  }
}

现在您需要获取该 CoffeeShop 的实例,但它只有一个带有依赖项的两个参数构造函数.那你怎么做呢?简单:您告诉 Dagger 在它生成的 Component 实例上提供一个 工厂方法.

Now you need to get an instance of that CoffeeShop, but it only has a two-parameter constructor with its dependencies. So how do you do that? Simple: You tell Dagger to make a factory method available on the Component instance it generates.

@Component(modules = {/* ... */})
public interface CoffeeShopComponent {
  CoffeeShop getCoffeeShop();

  void inject(CoffeeService serviceToInject); // to be discussed below
}

当您调用 getCoffeeShop 时,Dagger 创建 Provider 以提供 LightRoastCoffee,创建 DefaultCashRegister,将它们提供给 Coffeeshop 构造函数,并将结果返回给您.恭喜,您是一家设备齐全的咖啡店的自豪拥有者.

When you call getCoffeeShop, Dagger creates the Provider<Coffee> to supply LightRoastCoffee, creates the DefaultCashRegister, supplies them to the Coffeeshop constructor, and returns you the result. Congratulations, you are the proud owner of a fully-wired-up coffeeshop.

现在,所有这些都是替代 void 注入方法,它接受一个已经创建的实例并将其注入其中:

Now, all of this is an alternative to void injection methods, which take an already-created instance and inject into it:

public class CoffeeService extends SomeFrameworkService {
  @Inject CoffeeShop coffeeShop;

  @Override public void initialize() {
    // Before injection, your coffeeShop field is null.
    DaggerCoffeeShopComponent.create().inject(this);
    // Dagger inspects CoffeeService at compile time, so at runtime it can reach
    // in and set the fields.
  }

  @Override public void alternativeInitialize() {
    // The above is equivalent to this, though:
    coffeeShop = DaggerCoffeeShopComponent.create().getCoffeeShop();
  }
}

所以,你有它:两种不同的风格,这两种风格都可以让你访问完全注入的对象图,而无需列出或关心它们究竟需要哪些依赖项.您可以偏爱其中一种,或者偏爱用于 Android 或服务用例的顶级和成员注入的工厂方法,或任何其他类型的混合和匹配.

So, there you have it: Two different styles, both of which give you access to fully-injected graphs of objects without listing or caring about exactly which dependencies they need. You can prefer one or the other, or prefer factory methods for the top-level and members injection for Android or Service use-cases, or any other sort of mix and match.

(注意:除了用作对象图的入口点之外,称为提供方法的无参数 getter 也可用于公开组件依赖项的绑定,如David Rawson 在其他答案中进行了描述.)

(Note: Beyond their use as entry points into your object graph, no-arg getters known as provision methods are also useful for exposing bindings for component dependencies, as David Rawson describes in the other answer.)

这篇关于Dagger 2 组件中的 getter 方法的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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