title: Looking for an example for Dagger assisted injection
body: <p>From <a href="https://groups.google.com/forum/#!topic/dagger-discuss/QgnvmZ-dH9c" rel="noreferrer">dagger-discuss@</a>:</p>
  
  <p>I have a class that gets some dependencies from the object graph, and other dependencies from a caller at runtime.</p>
  
  <pre><code>public class ImageDownloader {
    // Get these dependencies from the injector.
    private final HttpClient httpClient;
    private final ExecutorService executorService;
  
    // Get these from the caller.
    private final URL imageUrl;
    private final ImageCallback callback;
  
    ...
  }
  </code></pre>
  
  <p>I came up with a solution, where I define a Factory,</p>
  
  <pre><code>public class ImageDownloader {
    ...
    public static class Factory {
      private final HttpClient httpClient;
      private final ExecutorService executorService;
  
      @Inject
      public Factory(HttpClient httpClient, ExecutorService executorService) {
        this.httpclient = httpClient;
        this.executorService = executorService;
      }
  
      public ImageDownloader create(URL imageUrl, ImageCallback callback) {
        return new ImageDownloader(httpClient, executorService, iamgeUrl, callback);
      }
    }
    ...
  }
  </code></pre>
  
  <p>Now, instead of injecting <code>ImageDownloader</code> in the client's constructor, I simply inject <code>ImageDownloader.Factory</code> and call its <code>create()</code> method.</p>
  
  <p>As you can see, that's quite verbose and long. It also has a bunch of duplication and boilerplate. There're some obstacles to annotating the fields themselves with <code>@Inject</code>, so let's ignore this possibility for now.</p>
  
  <p>The Square people have come up with an interesting solution, using providers. Define a <code>Factory</code> interface,</p>
  
  <pre><code>public class ImageDownloader {
    ...
    public interface Factory {
      ImageDownloader create(URL imageUrl, ImageCallback callback);
    }
  }
  </code></pre>
  
  <p>and then provide it in a module,</p>
  
  <pre><code>public class ImageModule {
    ...
    @Provides 
    public ImageModule.Factory provideImageModuleFactory(
        final Provider&lt;HttpClient&gt; httpClientProvider, 
        final Provider&lt;ExecutorService&gt; executorServiceProvider) {
      return new ImageDownloader.Factory() {
        public ImageDownloader create(URL imageUrl, ImageCallback callback) {
          return new ImageDownloader(httpClientProvider.get(), executorServiceProvider.get(),
              imageUrl, callback);
        }
    }
    ...
  }
  </code></pre>
  
  <p>(again, from dagger-discuss@).</p>
  
  <p>My <code>ImageDownloader</code> is a class that's injected by a class which is injected by another class which is injected by yet another class, ..., which is referenced in a <code>@Module</code>. This all somehow* works, and all classes are found in build time. Now, to add a module, I have to explicitly let the object graph know about it.</p>
  
  <p>I must be missing something - it's very easy to inject a new class, but very tedious to add a new module.</p>
  
  <p>My question is: how is assisted injection done in practice? anyone has an example? how should I use <code>ImageModule</code>, if at all?</p>
  
  <p>* - "somehow" does indeed imply it's partly magic to me.</p>