如何使用Dagger2实例化具有动态成员的对象? [英] How to Instantiate an object with dynamic members with Dagger2?

查看:282
本文介绍了如何使用Dagger2实例化具有动态成员的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在构造函数中获取参数的类:

I have a class which gets arguments in a constructor:

public class Dependency{
  Dependency(int number1, int number2, int number3, DependencyListener listener){}     
}

每个依赖类需要路径不同的参数,以实例化依赖项。
我应该如何定义模块和其他类可以正确启动它的组件,同时传输不同的值并将'this'作为监听器传递?

each dependent class needs to path different arguments in order to instantiate a dependency. How should i define the module and the component that other classes could initiate it properly while transferring different values and passing 'this' as the listener?

此外,在这种情况下我应该如何使用@Inject方法?

Also, How should i use the @Inject method in this case?

编辑1

@ jeff-bowman
我在考虑使用以下方法:

@jeff-bowman I was thinking of using the following method:

@Module public class DependencyModule {
  int first;
  int second;
  int third;
  DependencyListener listener;

  public DependencyModule(int first, int second, int third, DependencyListener listener) {
    this.first = first;
    this.second = second;
    this.third = third;
    this.listener = listener
  }

  @Provides Dependency provideDependency(int first, int second, int third, DependencyListener listener) {
    return new Dependency(first, second, third, listener)
  }
}

@Component(modules = { DependencyModule.class }) public interface     
DependencyComponent {
  void inject(DependentClass1 target);
  void inject(DependentClass2 target);
  void inject(DependentClass3 target);
}

我会在每个DependentClass中执行:

and in each DependentClass i would do:

public class DependentClass{
  @Inject Dependency;
  public DependentClass{
      DaggerDependencyComponent.builder().dependencyModule(new DependencyModule(first, second, third, this)).build().inject();
  }
}

这是一个好习惯吗?

推荐答案

从概念上讲,你想要的是工厂:

Conceptually, what you want is a Factory:

/** Inject this wherever you want an instance of Dependency. */
public interface DependencyFactory {
  Dependency create(
      int number1,
      int number2,
      int number3,
      DependencyListener listener);
}

public class DependencyFactoryImpl implements DependencyFactory {
  @Inject Provider<SomeDaggerDepIfNeeded> someDaggerDepProvider;

  @Override public void create(
      int number1,
      int number2,
      int number3,
      DependencyListener listener) {
    return new Dependency(number1, number2, number3, listener,
        someDaggerDepProvider.get() /* if necessary */);
  }
}

但是,因为这很容易自动生成,通常有内置工具可以做到这一点。 Guice 称之为辅助注射,并提供 FactoryModuleBuilder (通过可选的扩展JAR),在运行时反射生成工厂。 Dagger 2 没有内置的等价物,主要是因为谷歌已经发布了一个名为 AutoFactory 任何生成工厂 JSR-330 实现包括Dagger,Guice和Spring。

However, because this is so easy to generate automatically, there are often built-in tools to do so. Guice calls this Assisted Injection, and supplies FactoryModuleBuilder (via an optional extension JAR) that reflectively generates factories at runtime. Dagger 2 doesn't have a built-in equivalent, mostly because Google has already released an open-source JSR-330 factory generator called AutoFactory that generates a factory for any JSR-330 implementation including Dagger, Guice, and Spring.

@AutoFactory
public class Dependency{
  Dependency(
      int number1, int number2, int number3, DependencyListener listener,
      @Provided SomeDaggerDepIfNeeded somedaggerDep){}     
}

请参阅其呼叫详细信息AutoFactory文档,特别是如果您对生成的工厂实现接口感兴趣。使用您明确定义的接口可以更容易地在IDE中使用生成的代码。

See the AutoFactory documentation for details on its calls, particularly if you're interested in having the generated factory implement an interface. Using an interface you define explicitly can make it easier to work with generated code in IDEs.

(如果您不需要Dagger的依赖关系,或者替换对于依赖它的类,这个类的其他实现,你可以完全保留类,并将调用 new 作为工厂。)

(If you don't have any need for dependencies from Dagger, or for substituting other implementations of this class to classes that depend on it, you can leave the class exactly as it is and treat the call to new as a factory.)

这篇关于如何使用Dagger2实例化具有动态成员的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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