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

查看:109
本文介绍了匕首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注入()方法。但是我不明白其他 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 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.

让我们做一个咖啡店,这取决于一个供应商< Coffee> 和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的实例,但它只有一个具有依赖关系的双参数构造函数。那么你怎么做呢简单:你告诉匕首在其生成的组件实例上使用工厂方法

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< Coffee> 提供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.)

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

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