This repository has been archived by the owner on Aug 31, 2019. It is now read-only.
forked from google/guice
-
Notifications
You must be signed in to change notification settings - Fork 1
InjectOnlyDirectDependencies
sameb edited this page Jul 7, 2014
·
1 revision
Avoid injecting an object only as a means to get at another object. For example, don't inject a Customer
as a means to get at an Account
:
public class ShowBudgets {
private final Account account;
@Inject
ShowBudgets(Customer customer) {
account = customer.getPurchasingAccount();
}
Instead, inject the dependency directly. This makes testing easier; the test case doesn't need to concern itself with the customer. Use an @Provides
method in your Module
to create the binding for Account
that uses the binding for Customer
:
public class CustomersModule extends AbstractModule {
@Override public void configure() {
...
}
@Provides
Account providePurchasingAccount(Customer customer) {
return customer.getPurchasingAccount();
}
By injecting the dependency directly, our code is simpler.
public class ShowBudgets {
private final Account account;
@Inject
ShowBudgets(Account account) {
this.account = account;
}
- User's Guide
- Best Practices
- Frequently Asked Questions
- Integration
- Extensions
- Internals
- Releases
- Community