Can I use some kind of assisted Inject with Dagger?

后端 未结 3 1668
無奈伤痛
無奈伤痛 2020-12-04 02:57

With Google Guice or Gin I can specify parameter with are not controlled by the dependency injection framework:

class SomeEditor {


  @Inject
  public Some         


        
3条回答
  •  误落风尘
    2020-12-04 03:39

    Just like @xsveda, I also wrote an answer about this in this other question, which I'll also reproduce here.


    Today, for assisted injection with Dagger you probably want to use AssistedInject. I wrote about it in this blogpost, but I'll add a full example here to make things easier.

    First thing you need are the dependencies:

    compileOnly 'com.squareup.inject:assisted-inject-annotations-dagger2:0.4.0'
    kapt 'com.squareup.inject:assisted-inject-processor-dagger2:0.4.0'
    

    Then here's how it can look like:

    class ImageDownloader @AssistedInject constructor(
      private val httpClient: HttpClient,
      private val executorService: ExecutorService,
      @Assisted private val imageUrl: URL,
      @Assisted private val callback: ImageCallback
    ) {
    
      @AssistedInject.Factory
      interface Factory {
        fun create(imageUrl: URL, callback: ImageCallback): ImageDownloader
      }
    }
    

    First thing is that instead of annotating the constructor with @Inject, we annotate it with @AssistedInject. Then we annotate the parameters that will have to go through the factory, which is the opposite of what AutoFactory expects. Finally, we need an inner factory interface annotated with @AssistedInject.Factory that has a single method that receives the assisted parameters and returns the instance we're interested in.

    Unfortunately, we still have an extra step here:

    @AssistedModule
    @Module(includes = [AssistedInject_AssistedInjectModule::class])
    interface AssistedInjectModule
    

    We don't necessarily need a dedicated module for it, even though that's a valid option. But we can also have those annotations in another module that is already installed in the component. The nice thing here is that we only need to do it once, and after that any factory will automatically become part of the graph.

    With that, you can basically inject the factory and ask for your object as you'd normally do.

提交回复
热议问题