question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: factory method in components for assisted injection

See original GitHub issue

It is a common case when a class have some dynamic parameters along with usual dependencies. For example articleId is a dynamic parameter whereas articleService is an usual dependency.

class ArticlePresenter {
	...
	ArticlePresenter(long articleId, ArticleService articleService) {
		...
	}
}

This mixing of parameters and dependencies is known as Assisted injection. Dagger 2 does not support this feature now.

There are three work-around known to me:

  1. Use factories (probably generated by third-party libraries).
  2. Create a separate component for a class with dynamic parameters.
  3. Set dynamic parameters with setters after an object is created.

In my opinion, all of these aproaches are far from ideal. So I propose a new feature “Factory method in components” for support of assisted injection.

Lets allow components to have getSomething methods with parameters. Like that:

@Component
public interface AppComponent {
	ArticlePresenter getArticlePresenter(long articleId);
}

Add @Param annotation to specify which constructor arguments are dynamic parameters:

@Inject
public ArticlePresenter(@Param long articleId, ArticleService articleService) {
	...
}

After that we can request objects from the component:

ArticlePresenter presenter = appComponent.getArticlePresenter(someId);

Of course, this is just an idea not a complete solution. What do you think, guys? Will it make Dagger 2 even more awesome?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:24
  • Comments:5

github_iconTop GitHub Comments

18reactions
ronshapirocommented, Nov 15, 2017

I’ve been thinking about this problem a lot, mostly in the context of two concepts:

  1. AutoFactory is an API-generating processor, which is usually less preferred vs. implementation generating.
  2. AutoFactory forces Provider objects for each dependency - fine for servers, but bad in client apps.

I think Dagger could provide a nice intermediary and we could design something that was still JSR 330 compatible (and easy to use within Guice, Spring, etc). The Dagger flavor could take advantage of the inlining work we’ve done recently and be a much lighter weight factory.

The one downside in my current design is the factory would hold an implicit reference back to the component and could be a memory leak. But there’s more to think about this. I’ll post back when I’ve thought about it more.

8reactions
ronshapirocommented, Sep 4, 2018

@cgdecker is working on something like this

Read more comments on GitHub >

github_iconTop Results From Across the Web

Assisted Injection - Dagger
Assisted injection is a dependency injection (DI) pattern that is used to ... A factory is typically responsible for combining all of the...
Read more >
Is it possible and how to do Assisted Injection in Spring?
In Spring you can have Instantiation using a static factory method or Instantiation using an instance factory method.
Read more >
Leveraging AssistedInjection to inject ViewModels with ...
we create a module that uses @Binds @IntoMap @WorkerKey to collect each __Worker.Factory as an AssistedWorkerFactory in our Dagger component.
Read more >
Assisted Injection for Dagger - Google Groups
Guice has an extension AssistedInject to solve this problem. They let you define a custom Factory interface that replaces Provider . You use...
Read more >
Inversion of Control Containers and the Dependency Injection ...
A new step is to register the injectors that will inject the dependent components. Each injection interface needs some code to inject the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found