将外部依赖的构造函数传递到Guice实现中

huangapple go评论95阅读模式
英文:

Passing constructors for outer dependency into Guice implementation

问题

我有一个工作,需要从深层存储中读取数据。我正在使用Guice DI来进行项目开发。

已经编写了一个深层存储,并且作为外部依赖项提供。我在实例化Guice中的客户端方面遇到了困难。

以下是代码示例:

JobModule

  1. public class JobModule extends AbstractModule {
  2. private Config config;
  3. JobModule(Config config) {
  4. this.config = config;
  5. }
  6. @Override
  7. protected void configure() {
  8. bind(Reader.class).to(DeepStoreReader.class);
  9. }
  10. @Provides
  11. @Named("config")
  12. Config provideConfig() {
  13. return this.config;
  14. }
  15. }

Reader 接口

  1. public interface Reader {
  2. List<String> getData(String path);
  3. }

DeepStoreReader

  1. public class DeepStoreReader implements Reader {
  2. private final DeepStoreClient deepStoreClient;
  3. DeepStoreReader(@Named("config") Config config) {
  4. this.deepStoreClient = new DeepStoreClient(config);
  5. }
  6. @Override
  7. public List<String> getData(String path) {
  8. return this.deepStoreClient.getData(path);
  9. }
  10. }

问题是我不想在 DeepStoreReader 构造函数内实例化 DeepStoreClient,因为这会使得测试 DeepStoreReader 变得困难,因为我将无法对 DeepStoreClient 进行模拟。

在这种情况下,实例化客户端的首选方式是什么?DeepStoreClient 不是一个 Guice 模块/实现,而是作为外部已发布的依赖项提供的。

PS:我对依赖注入不太熟悉,正在学习 Guice。

英文:

I have a Job, which should read data from deep storage. I am using Guice DI for my project.

There is a deep store already written and coming as an outer dependencie. I am struggling with instantiating the client in Guice

Here is the code

JobModule

  1. public class JobModule extends AbstractModule {
  2. private Config config;
  3. JobModule(Config config) {
  4. this.config = config;
  5. }
  6. @Override
  7. protected void configure() {
  8. bind(Reader.class).to(DeepStoreReader.class);
  9. }
  10. @Provides
  11. @Named(&quot;config&quot;)
  12. Config provideConfig() {
  13. return this.config;
  14. }
  15. }

Reader Interface

  1. public interface Reader {
  2. List&lt;String&gt; getData(String path);
  3. }

DeepStoreReader

  1. public class DeepStoreReader implements Reader {
  2. private final DeepStoreClient deepStoreClient;
  3. DeepStoreReader(@Named(&quot;config&quot;) Config config) {
  4. this.deepStoreClient = new DeepStoreClient(config);
  5. }
  6. @Override
  7. public List&lt;String&gt; getData(String path) {
  8. return this.deepStoreClient.getData(path);
  9. }
  10. }

The issue is I don't want to instantiate DeepStoreClient inside the DeepStoreReader constructor, because it becomes difficult to test DeepStoreReader, since I won't be able to mock DeepStoreClient

What is the preferred way to instantiate a client in such cases? DeepStoreClient is not a Guice module/implementation and is coming as an outer published dependency

PS: I am new to DI and learning Guice

答案1

得分: 1

你想要的是构造函数注入,例如:

  1. @Inject
  2. public DeepStoreReader(DeepStoreClient deepStoreClient) {
  3. this.deepStoreClient = deepStoreClient;
  4. }

Guice 将会负责为你实例化 DeepStoreClient

编辑:

如果 DeepStoreClient 本身有依赖关系,你也可以注解那个构造函数:

  1. @Inject
  2. public DeepStoreClient(@Named("config") Config config) {
  3. // ... 8< ...
  4. }
英文:

What you want is constructor injection, e.g.:

  1. @Inject
  2. public DeepStoreReader(DeepStoreClient deepStoreClient) {
  3. this.deepStoreClient = deepStoreClient;
  4. }

Guice will take care of instantiating the DeepStoreClient for you.

EDIT:

If DeepStoreClient itself has dependencies, you can also annotate that constructor:

  1. @Inject
  2. public DeepStoreClient(@Named(&quot;config&quot;) Config config) {
  3. // ... 8&lt; ...
  4. }

huangapple
  • 本文由 发表于 2020年9月15日 22:05:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63903661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定