根据模块的匕首另一个模块 [英] Module depending on another module in Dagger

查看:121
本文介绍了根据模块的匕首另一个模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用匕首以做我正在创建一个应用程序依赖注入和运行陷入困境构造适当的DAG时,我有取决于由喷油器(由另一个模块psumably提供$ p $)提供的值一个封装的模块。

I'm trying to use Dagger to do Dependency Injection on an app that I'm building, and running into trouble constructing proper DAGs when I have one package's Module depending on values provided by the Injector (presumably provided by another Module).

如果我有一些配置变量,一个简单的模块(即我可能想换出测试环境,例如)

If I have a simple module for some configurable variables (that I might want to swap out for testing environments, for example)

@Module(
    injects = DependentModule.class,
)
public class ConfigModule {

    @Provides @Named("ConfigOption") String provideConfigOption() {
        return "This Module's configurable option!";
    }
}

和另一个模块依赖于它,例如

and another module depends on it, e.g.

@Module(
    injects = {
            TopLevelClass.class
    }
)
public class DependentModule {

    @Inject @Named("ConfigOption") String configOption;

    public DependentModule() {
        ObjectGraph.create(this).inject(this);
        doSomethingWithConfig(configOption);
    }

    @Provides @Singleton UsefulValue provideUsefulValue() {
        // Whatever this module needs to do...
    }
}

在我尝试在构造函数中未能引导注射就行了,它抱怨我没有指定明确内喷射适当的模块线。

经过反复试验和错误我看到这个消失如果 @Module 我加一行 =包含ConfigModule.class ,但是这给我的印象语义错误的,因为)我将创建DAG现在将包括两个模块的值,而不是只有一个,和b)它击败第一DI的目的/灵活性放置链接特定的模块,而不是简单地让匕首注入相应的值。

Through trial-and-error I see this goes away if in @Module I add a line include = ConfigModule.class, but this strikes me as semantically wrong, since a) the DAG I'll be creating will now include the values of both modules, rather than just one, and b) it defeats the purpose/flexibility of DI in the first place to link a specific Module rather than simply let Dagger inject the appropriate value.

我presuming我不应该创建具有的对象图这种只注入了吗?但后来我遇到不连接特定模块的问题...

I'm presuming I shouldn't be creating an Object Graph with this only to inject into it? But then I run into the issue of not linking a specific Module...

为简洁:


  • 什么是适当的方式来注入值成可以从其它模块提供一个的模块?在这里,我使用的字段注入,但我与构造注入实验也导致了很多失败的。

  • 与此相关的,什么时候适合使用 addsTo 包括

  • What is the 'proper' way to Inject values into one Modules that may be provided from other Modules? Here I'm using field injection, but my experiments with constructor injection have also resulted in a lot of failure.
  • Relatedly, when is it appropriate to use addsTo vs. includes?

感谢:)

推荐答案

您不需要从另一个明确一个模块中做任何注入(字段或构造函数)的。只需使用 addsTo 包括
包括允许模块添加到另一个使用他们提供的一切。例如:

You don't need to do any of injection (field or constructor) in one module from another explicitly. Just use addsTo and includes. includes allows to add modules to another and use everything they provide. Example:

@Module()
public class ModuleA {
    @Provides @Named("ValueA") String provideValueA() {
        return "This is ValueA";
    }
}

@Module(
    includes = ModuleA.class
)
public class ModuleB {
    // ValueA comes from ModuleA
    @Provides @Named("ValueB") String provideValueB(@Named("ValueA") String valueA) {
        return valueA + " and ValueB";
    }
}

addsTo 用于与 ObjectGraph.plus(对象...模块)。当已创建图形,并包含一些模块(例如,在应用类),您可以创建新的使用图形(例如,在活动)加<​​/ code>。例如:

addsTo is used with ObjectGraph.plus(Object... modules). When graph is already created and contains some modules (e.g. in Application class), you can create new graph (e.g. in Activity) using plus. Example:

@Module()
public class ApplicationModule {
    @Provides @Named("ValueA") String provideValueA() {
        return "This is ValueA";
    }
}

@Module(
    addsTo = ApplicationModule.class
)
public class ActivityModule {
    // ValueA comes from ApplicationModule
    @Provides @Named("ValueB") String provideValueB(@Named("ValueA") String valueA) {
        return valueA + " and ValueB";
    }
}

public class DemoApplication extends Application {
  private ObjectGraph graph;

  @Override public void onCreate() {
     super.onCreate();
     graph = ObjectGraph.create(getModules().toArray());
  }

  protected List<Object> getModules() {
      return Arrays.asList(
          new ApplicationModule()
      );
  }

  public void inject(Object object) {
      graph.inject(object);
  }

  public ObjectGraph getObjectGraph() {
      return graph;
  }
}

public class DemoActivity extends Activity {
    private ObjectGraph activityGraph;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create the activity graph by .plus-ing our modules onto the application graph.
        DemoApplication application = (DemoApplication) getApplication();
        activityGraph = application.getApplicationGraph().plus(new ActivityModule());

        // Inject ourselves so subclasses will have dependencies fulfilled when this method returns.
        activityGraph.inject(this);
    }

    @Override protected void onDestroy() {
        // Eagerly clear the reference to the activity graph to allow it to be garbage collected as
        // soon as possible.
        activityGraph = null;
        super.onDestroy();
    }
}

你也可以检查这个例如创建图表的范围。

这篇关于根据模块的匕首另一个模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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