google/dagger

Following dagger.dev tutorial doesnt create singleton in v2.51

AAlfarho opened this issue · 2 comments

Following https://dagger.dev/tutorial/09-maintaining-state tutorial you create a singleton instance of a Database class.

Using Dagger v2.51 and @Singleton annotations on the Database class and @Component, still results in 2 instances of Database the generated code looks like this

    private LoginCommand loginCommand() {
      return new LoginCommand(SystemOutModule_TextOutputterFactory.textOutputter(), new Database());
    }

    private DepositCommand depositCommand() {
      return new DepositCommand(SystemOutModule_TextOutputterFactory.textOutputter(), new Database());
    }

link to repo

Changing the dagger version to 2.23.2 does generate a singleton of Database, generate code

  private DepositCommand getDepositCommand() {
    return new DepositCommand(SystemOutModule_TextOutputterFactory.textOutputter(), databaseProvider.get());}

  private Map<String, Command> getMapOfStringAndCommand() {
    return MapBuilder.<String, Command>newMapBuilder(3).put("hello", getHelloWorldCommand()).put("login", getLoginCommand()).put("deposit", getDepositCommand()).build();}

  @SuppressWarnings("unchecked")
  private void initialize() {
    this.databaseProvider = DoubleCheck.provider(Database_Factory.create());
  }

link to repo

Im very new to dagger, has the behavior change between versions?

Hi, can you double check if you got the version right? I patched your repro, and I'm seeing the databaseProvider.get() output for 2.5.1