Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 这是在Dagger 2中向构造函数添加参数的正确方法吗?_Android_Dependency Injection_Dagger 2 - Fatal编程技术网

Android 这是在Dagger 2中向构造函数添加参数的正确方法吗?

Android 这是在Dagger 2中向构造函数添加参数的正确方法吗?,android,dependency-injection,dagger-2,Android,Dependency Injection,Dagger 2,上下文 最近我开始调查依赖注入和匕首2。它看起来是一个相当不错的图书馆,但对我来说似乎有点困惑。有些情况下,我不知道该如何进行 我试过什么 我创建了一个简单的Android应用程序,它创建了一个客户端及其依赖项,并做了一些虚拟工作。这些是课程: Client.java public class Client { private Dependency dep; @Inject public Client(Dependency dep) { this.de

上下文

最近我开始调查依赖注入和匕首2。它看起来是一个相当不错的图书馆,但对我来说似乎有点困惑。有些情况下,我不知道该如何进行

我试过什么

我创建了一个简单的Android应用程序,它创建了一个客户端及其依赖项,并做了一些虚拟工作。这些是课程:

Client.java

public class Client {

    private Dependency dep;

    @Inject
    public Client(Dependency dep) {
        this.dep = dep;
    }

    public void work() {
        System.out.println("Client working");
        dep.doWork();
    }
}
public class Client {

    int id;
    Dependency dep;

    @Inject
    public Client(int id, Dependency dep) {
        this.id = id;
        this.dep = dep;
    }

    public void work() {
        System.out.println("id: " + id + " Client working");
        dep.doWork();
    }
}
Dependency.java

public class Dependency {

    @Inject
    public Dependency() {
    }

    public void doWork() {
        System.out.println("Dependency working");
    }
}
public class Dependency {

    private int id;

    @Inject
    public Dependency(int id) {
        this.id = id;
    }

    public void doWork() {
        System.out.println("id: " + id + " Dependency working");
    }
}
在一些教程之后,我创建了两个模块类:

DependencyModule.java

@Module
public class DependencyModule {

    @Provides
    Dependency provideDependency() {
        return new Dependency();
    }
}
@Module
public class ClientModule {

    @Provides
    Client provideClient(Dependency dep) {
        return new Client(dep);
    }

}
ClientModule.java

@Module
public class DependencyModule {

    @Provides
    Dependency provideDependency() {
        return new Dependency();
    }
}
@Module
public class ClientModule {

    @Provides
    Client provideClient(Dependency dep) {
        return new Client(dep);
    }

}
以及组件接口:

@Component(modules = {ClientModule.class})
public interface ClientComponent {

    Client provideClient();

}
这个很好用。通过我的活动,我可以做到以下几点:

ClientComponent clientComp = DaggerClientComponent
                .builder()
                .clientModule(new ClientModule())
                .build();

Client client = clientComp.provideClient();
client.work();
问题

我知道如何在客户机中注入依赖项,至少我这么认为。但是我如何将参数添加到客户机/依赖项的构造函数中呢

我是说,如果我想给我的对象添加一些int参数呢?像这样简单的事情:

Client.java

public class Client {

    private Dependency dep;

    @Inject
    public Client(Dependency dep) {
        this.dep = dep;
    }

    public void work() {
        System.out.println("Client working");
        dep.doWork();
    }
}
public class Client {

    int id;
    Dependency dep;

    @Inject
    public Client(int id, Dependency dep) {
        this.id = id;
        this.dep = dep;
    }

    public void work() {
        System.out.println("id: " + id + " Client working");
        dep.doWork();
    }
}
Dependency.java

public class Dependency {

    @Inject
    public Dependency() {
    }

    public void doWork() {
        System.out.println("Dependency working");
    }
}
public class Dependency {

    private int id;

    @Inject
    public Dependency(int id) {
        this.id = id;
    }

    public void doWork() {
        System.out.println("id: " + id + " Dependency working");
    }
}
注: 下面的代码是我尝试过的。所以我不确定它的正确性

因此,由于对象的构造函数中有新参数,因此模块必须更改:

DependencyModule.class

public class DependencyModule {

    @Provides
    Dependency provideDependency() {
        return new Dependency(id);
    }
}
@Module
public class ClientModule {

    @Provides
    Client provideClient(int id, Dependency dep) {
        return new Client(id, dep);
    }

}
ClientModule.class

public class DependencyModule {

    @Provides
    Dependency provideDependency() {
        return new Dependency(id);
    }
}
@Module
public class ClientModule {

    @Provides
    Client provideClient(int id, Dependency dep) {
        return new Client(id, dep);
    }

}
问题:

如何使用新模块?我还没有找到将id传递给该方法的方法。我让它工作的唯一方法是将它传递到模块构造函数中,并将其从提供方法中删除。这样:

@Module
public class ClientModule {

    private int id;

    public ClientModule(int id) {
        this.id = id;
    }

    @Provides
    Client provideClient(Dependency dep) {
        return new Client(id, dep);
    }

}
在DependencyModule.java中使用相同的方法

这样,在ClientComponent接口中添加DependencyModule.class,我可以执行以下操作:

ClientComponent clientComp = DaggerClientComponent
                .builder()
                .clientModule(new ClientModule(clientId))
                .dependencyModule(new DependencyModule(dependencyId))
                .build();

Client client = clientComp.provideClient();
client.work();
这是正确的做法吗

有没有更好的方法达到同样的效果


我犯了违反DI原则的罪行吗?

有两种基本方法可以让Dagger提供类的实例:

将@Inject添加到构造函数中,并将类的依赖项作为构造函数参数放入

向@Module注释类添加@Provides注释方法,并将该模块安装到@Component中

每个类只需要使用一个方法。所以在第一个例子中,客户机和依赖关系都很好;您也不需要ClientModule和DependencyModule

添加int依赖项后,现在确实需要一个模块,因为没有类可以@Inject。模块只需要提供int,这样类似的东西就可以工作了:

@Module
public class ClientIdModule {
  private final clientId;

  public ClientIdModule(int clientId) {
    this.clientId = clientId;
  }

  @Provides
  static int clientId() {
    return clientId;
  }
}
现在,如果将ClientIdModule安装到组件中,您将能够获得一个具有正确ID的客户端,并且它的依赖关系也将随之改变