Injecting Dependencies From Base Class
See original GitHub issueIn Dagger 1, you could call inject
from a base class and have all dependencies satisfied for subclasses:
class BaseActivity extends Activity {
@Inject Foo foo;
public void onCreate(Bundle savedInstanceState) {
objectGraph().inject(this);
}
}
class MainActivity extends BaseActivity {
@Inject Bar bar;
@Override void onCreate(Bundle savedInstanceState) {
super.onCreate();
// both foo and bar will be available here
}
}
This has changes in Dagger 2; injecting from a base class will only satisfy the base class’s dependencies, not it’s subclasses’s.
class BaseActivity extends Activity {
@Inject Foo foo;
public void onCreate(Bundle savedInstanceState) {
component().inject(this);
}
}
class MainActivity extends BaseActivity {
@Inject Bar bar;
@Override void onCreate(Bundle savedInstanceState) {
super.onCreate();
// only foo is available here, not bar
}
}
Is there a recommended pattern for injecting dependencies from a base class, rather than calling inject in each of the subclasses?
Issue Analytics
- State:
- Created 9 years ago
- Comments:22
Top Results From Across the Web
Handling Dependency Injection in Inherited Classes
In this post I have described a simple method for managing dependency injection in inherited classes, by creating a dependency aggregate class ......
Read more >Implicitly injecting dependency in Base class while derived ...
I thought that Dependency dep should get injected automatically(through Constructor Injection) when derived class is resolved. But this doesnt seem to work when ......
Read more >3 Exciting Methods for Dependency Injection With Inheritance ...
Dependency injection is a way to inject dependencies into a class for use in its methods. With dependency injection, the class becomes loosely...
Read more >Dependency injection - .NET | Microsoft Learn
.NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between ...
Read more >Dependency Injection and Class Inheritance - Peter's Pattern
Dependency Injection and Class Inheritance. Once upon a project there was a base class: class abstract CommonLogic { protected ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here’s how members injection methods work:
@Inject
anywhere in its class hierarchy. If it doesn’t, you’ll get an error.@Inject
ed members in the entire type hierarchy will be injected: the argument type and all supertypes@Inject
ed for subtypes of the argument type.So, given the following:
The following test will pass:
Tried the example posted by @JakeWharton, and member in SubClass was not injected. However, as I explore more in the generated code, that could possibly be done via the following:
This way, bar is injected properly.